I'am try to make a session on a Textbox in asp.net, that textbox allows the user to enter values from 1-4, but whenever I click on button "book", it gives me an null pointer exception, I am using FindControl function from my c# backend.
Here's my code .aspx page
<%@ Page Title="" Language="C#" MasterPageFile="~/Main.Master" AutoEventWireup="true" CodeBehind="hotels_main.aspx.cs" Inherits="Hotel_Mangement.hotels_main" %>
<asp:Content ID="Content1" ContentPlaceHolderID="hotels_main" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="free website templates, hotel and travel, CSS, XHTML, design layout" />
<meta name="description" content="Hotel and Travel - free website template provided by templatemo.com" />
<link href="/css/templatemo_style.css" rel="stylesheet" type="text/css" />
<div id="templatemo_container">
<!-- Free Website Template by www.TemplateMo.com -->
<div id="templatemo_content">
<div id="content_left" style="background-color:#2C3E50">
<div class="content_left_section">
<div class="content_title_01">Confirm Your Booking</div>
<form id="form1" >
<div class="form_row">
<br /><br />
<big>Check in:</big>
<asp:Calendar ID="checkin" runat="server" OnDayRender="checkin_DayRender" ></asp:Calendar>
<br /><br />
<big>Check out:</big> <asp:Calendar ID="checkout" runat="server" OnDayRender="checkout_DayRender1" ></asp:Calendar>
<br /><br />
<big>Number of room:</big> <asp:TextBox ID="rooms" runat="server" ForeColor="black" placeholder="Number of rooms" type="number" min="1" max="2"></asp:TextBox>
<br /><br />
<big>Number of members per room:</big> <asp:TextBox ID="members" runat="server" ForeColor="black" placeholder="Number of members" type="number" min="1" max="4"></asp:TextBox>
<br /> <br />
<asp:Button ID="book" runat="server" Text="Book" ForeColor="Black" Width="200px" Height="30px" OnClick="book_Click"></asp:Button>
</div>
<div class="cleaner"> </div>
</div> <!-- end of booking -->
<div class="cleaner_h30"> </div>
<div class="cleaner_horizontal_divider_01"> </div>
<div class="cleaner_h30"> </div>
<div class="content_left_section">
<div class="content_title_01">Contact Details</div>
<div class="news_title">Address</div>
<p>The Taj Mahal Palace<br />Apollo Bunder, Near Gateway Of India, Colaba , Mumbai , Maharashtra , India<br />Pincode- 400001</p>
<div class="cleaner_h30"> </div>
<div class="news_title">Phone</div>
<p>022 6665 3366 </p>
<div class="cleaner_h20"> </div>
<div class="news_title">Email Support</div>
<p>reservations@tajhotels.com</p>
</div> <!-- end of news section -->
<div class="cleaner_h30"> </div>
<div class="cleaner_horizontal_divider_01"> </div>
<div class="cleaner_h30"> </div>
<div class="cleaner_h30"> </div>
</div> <!-- end of content left -->
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<div id="content_right">
<div class="content_right_section">
<div class="content_title_01">Welcome to The <asp:Label ID="lblhotelname" runat="server" Text='<%#Eval("hotel_name") %>'></asp:Label> </div>
<asp:Image ID="Image1" runat="server" ImageUrl='<%#Eval("ImagePath") %>' Height="200px" width="200px"/>
<p><%#Eval("d_desc") %>. </p>
</div>
<div class="cleaner_h40"> </div>
<div class="content_right_2column_box">
<div class="content_title_01">Hotel Facilities</div>
<p> <%#Eval("hotel_facilties") %></p>
</div>
<div class="content_right_2column_box">
<div class="content_title_01">Policies</div>
<ul>
<asp:Label ID="lblpolicies" runat="server" Text='<%#Eval("hotel_policies") %>'></asp:Label>
</ul>
<div class="cleaner_h10"> </div>
</div>
<div class="cleaner_h40"> </div>
<div class="content_right_section">
<div class="content_title_01">Hotel location</div>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("hotel_location") %>'></asp:Label>
<div class="cleaner_h20"> </div>
<div class="cleaner"> </div>
</div>
<div class="content_right_section">
<div class="content_title_01">Hotel price per room</div>
<asp:FormView ID="FormView1" runat="server"></asp:FormView>
<asp:Label ID="lblhotelprice" runat="server" Text='<%#Eval("price")%>'></asp:Label>
<div class="cleaner_h20"> </div>
<div class="cleaner"> </div>
</div>
<div class="cleaner_h20"> </div>
</div> <!-- end of content right -->
</ItemTemplate>
</asp:Repeater>
</div>
</div>
</asp:Content>
.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace Hotel_Mangement
{
public partial class hotels_main : System.Web.UI.Page
{
int id;
int t1main, t2main, totalmain;
public static List<DateTime> list = new List<DateTime>();
SqlConnection con = new SqlConnection(@"Data Source=RISHIK\SQLEXPRESS;Initial Catalog=Register;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["id"] == null)
{
Response.Redirect("hotels.aspx");
}
else
{
id = Convert.ToInt32(Request.QueryString["id"].ToString());
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from hotels_main where hotel_id=" + id + "";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
Repeater1.DataSource = dt;
Repeater1.DataBind();
con.Close();
}
}
protected void checkin_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.Date < DateTime.Now.Date)
{
e.Day.IsSelectable = false;
e.Cell.ForeColor = System.Drawing.Color.Red;
e.Cell.Font.Strikeout = true;
}
Session["checkinmain"] = list;
}
protected void checkout_DayRender1(object sender, DayRenderEventArgs e)
{
if ((e.Day.Date < DateTime.Now.Date) || (e.Day.Date < checkin.SelectedDate))
{
e.Day.IsSelectable = false;
e.Cell.ForeColor = System.Drawing.Color.Red;
e.Cell.Font.Strikeout = true;
}
Session["checkoutmain"] = list;
}
protected void book_Click(object sender, EventArgs e)
{
if (Session["USER_ID"] == null)
{
Response.Redirect("login.aspx");
}
else
{
TextBox rooms = (TextBox)FindControl("rooms");
Session["roomsmain"] = rooms.Text;
TextBox members = (TextBox)FindControl("members");
Session["membersmain"] = members.Text;
Label lblhotelprice = (Label)FindControl("lblhotelprice");
Session["initialprice"] = lblhotelprice.Text;
t1main = Convert.ToInt32(Session["roomsmain"].ToString());
t2main = Convert.ToInt32(Session["initialprice"].ToString());
totalmain = t1main * t2main;
Session["totalpricemain"] = totalmain;
con.Open();
string insertQuery = "insert into hotel_booking_details(username,hotel_name,hotel_location,check_in,check_out,members,rooms,initial_price,total_price) values('" + Session["USER_ID"].ToString() + "','" + Session["hotel_name"].ToString() + "','" + Session["hotel_location"].ToString() + "','" + Session["checkinmain"].ToString() + "','" + Session["checkoutmain"].ToString() + "','" + Session["membersmain"].ToString() + "','" + Session["roomsmain"].ToString() + "','" + Session["initialprice"].ToString() + "','" + Session["totalpricemain"].ToString() + "')";
SqlCommand cmd1 = new SqlCommand(insertQuery, con);
cmd1.ExecuteNonQuery();
con.Close();
Response.Redirect("pay.aspx");
}
}
}
}
First of all, you cannot use another form tag in ASP.Net. You will need to remove <form id="form1" >
.
it gives me an null pointer exception, I am using FindControl function from my c# backend.
TextBox rooms = (TextBox)FindControl("rooms");
About code tries to find rooms textbox inside Page control. rooms control is not a direct child of Page control.
You will need to the find control recursively. Here is the helper method -
public static Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
return root;
return root.Controls.Cast<Control>()
.Select(c => FindControlRecursive(c, id))
.FirstOrDefault(c => c != null);
}
TextBox rooms = (TextBox)FindControlRecursive(Page, "rooms");