Answered by:
what is the problem here please help!!!!!!!

Question
-
User-1758998371 posted
Hi everyone I have a problem and I hope to find solution with you
I am doing a small project about hotel rooms booking
Here I want to calculate the charge of the room by selecting the room charge from room table and get the different between the date in and out and multiply it .
In my project first
The customer selects the date in, date out and I take it as session
Then the customer get the list of available room the user select the room id
This room id I take it in session too.
third the customer get the booking details contain the room Id the date in and date out and the total charge
in booking details page I have to select
1- the room rate per day and multiply it with the muber of days the customer stay in the hotel
2 [select datediff(date in and date out)]
3-insert this details in bookingroom details
4-update the room status in room table
But I get the following errors:
Server Error in '/WebSite1' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 25: TextBox7.Text = Session["username"].ToString(); Line 26: //select the book id +1 Line 27: this.cmd.Connection = con; Line 28: this.cmd.CommandType = CommandType.Text; Line 29: this.cmd.CommandText = "SELECT MAX(Book_ID)+1 FROM BOOKING";
Source File: c:\Users\Al_Riyami\Documents\Visual Studio 2005\Projects\WebSite1\bookingdetails.aspx.cs Line: 27
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.] Default3.Page_Load(Object sender, EventArgs e) in c:\Users\Al_Riyami\Documents\Visual Studio 2005\Projects\WebSite1\bookingdetails.aspx.cs:27 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +37 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +91 System.Web.UI.Control.OnLoad(EventArgs e) +127 System.Web.UI.Control.LoadRecursive() +92 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3364
Version Information: Microsoft .NET Framework Version:2.0.50727.3603; ASP.NET Version:2.0.50727.3074 <!-- [NullReferenceException]: Object reference not set to an instance of an object. at Default3.Page_Load(Object sender, EventArgs e) in c:\Users\Al_Riyami\Documents\Visual Studio 2005\Projects\WebSite1\bookingdetails.aspx.cs:line 27 at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) at System.Web.UI.Control.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) [HttpUnhandledException]: Exception of type 'System.Web.HttpUnhandledException' was thrown. at System.Web.UI.Page.HandleError(Exception e) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) at System.Web.UI.Page.ProcessRequest(HttpContext context) at ASP.bookingdetails_aspx.ProcessRequest(HttpContext context) in c:\Users\Al_Riyami\AppData\Local\Temp\Temporary ASP.NET Files\website1\b4273aff\f037739f\App_Web_dbac4nqw.2.cs:line 0 at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) -->Thursday, December 17, 2009 4:15 AM
Answers
-
User-1758998371 posted
it solved
i get some codes from my class mate
and i modify it
i delivered my project before three hours it was good the teachers love it
it dose not show any error Wink
thanks for help Laughing- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, December 22, 2009 6:29 AM
All replies
-
User1692533347 posted
write this.cmd=new SQLCommand(); before using "cmd" object
Thursday, December 17, 2009 6:38 AM -
User-1758998371 posted
still it give me errors
see
that is my .aspx.cs code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class Default3 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=HOTEL;Persist Security Info=True;User ID=sa;Password=admin123");
SqlCommand cmd,cmd1,cmd2,cmd3,cmd4,cmd5 = new SqlCommand();
SqlDataAdapter ad,ad2,ad1,ad3,ad4 = new SqlDataAdapter();
DataSet ds,ds1,ds2,ds3,ds4 = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
TextBox5.Text = Session["roomid"].ToString();
TextBox3.Text = Session["indate"].ToString();
TextBox2.Text = Session["outdate"].ToString();
TextBox7.Text = Session["username"].ToString();
//select the book id +1
SqlCommand cmd = new SqlCommand();
this.cmd.Connection = con;
this.cmd.CommandType = CommandType.Text;
this.cmd.CommandText = "SELECT MAX(Book_ID)+1 FROM BOOKING";
con.Open();
this.ad.SelectCommand = cmd;
this.ad.Fill(ds);
this.TextBox6.Text = ds.Tables[0].Rows[0][0].ToString();
con.Close();
//select the customer ID
SqlCommand cmd1 = new SqlCommand();
this.cmd1.Connection = con;
this.cmd1.CommandType = CommandType.Text;
this.cmd1.CommandText = "SELECT Customer_ID FROM CUSTOMER WHERE userid ='" + TextBox7.Text + "'";
con.Open();
this.ad.SelectCommand = cmd1;
this.ad.Fill(ds);
this.TextBox6.Text = ds.Tables[0].Rows[0][0].ToString();con.Close();
// FIND THE NUMBER OF DAYS
SqlCommand cmd2 = new SqlCommand();
this.cmd2.Connection = con;
this.cmd2.CommandType = CommandType.Text;
this.cmd2.CommandText = "SELECT DATEDIFF('d','" + TextBox3.Text + "','" + TextBox2.Text + "')";
con.Open();
this.ad.SelectCommand = cmd2;
this.ad.Fill(ds);
this.TextBox8.Text= ds.Tables[0].Rows[0][0].ToString();con.Close();
//GET THE ROOM RATE
SqlCommand cmd3 = new SqlCommand();
this.cmd3.Connection = con;
this.cmd3.CommandType = CommandType.Text;
this.cmd3.CommandText = "SELECT Room_Rate from ROOM WHERE Room_ID='"+TextBox5.Text+"')";
con.Open();
this.ad.SelectCommand = cmd3;
this.ad.Fill(ds);
this.TextBox7.Text = ds.Tables[0].Rows[0][0].ToString();con.Close();
int days;
if (int.TryParse(TextBox8.Text.Trim(), out days) == false)
{
days = 0;
}
int rate;
if (int.TryParse(TextBox7.Text.Trim(), out rate) == false)
{
rate = 0;
}
int total = days * rate;this.TextBox1.Text = total.ToString();
Session["Reservation_id"] = TextBox6.Text;
}
protected void TextBox6_TextChanged(object sender, EventArgs e)
{}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=HOTEL;Persist Security Info=True;User ID=sa;Password=admin123");
DataSet ds = new DataSet();
SqlDataAdapter ad = new SqlDataAdapter();
DataTable dt = new DataTable();
SqlCommand cmd4 = new SqlCommand();
this.cmd4.Connection = con;
this.cmd4.CommandType = CommandType.Text;
this.cmd4.CommandText = "update ROOM set Room_Status='not free' where Room_ID='" + Convert.ToInt16(TextBox5.Text) + "'";
con.Open();
this.cmd.ExecuteNonQuery();
con.Close();
SqlCommand cmd5 = new SqlCommand();
this.cmd5.Connection = con;
this.cmd5.CommandType = CommandType.Text;
this.cmd5.CommandText = "insert into BOOKING values('" + TextBox6.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox3.Text+ "','" +TextBox2.Text + "','" + TextBox1.Text + "')";
con.Open();
this.cmd5.ExecuteNonQuery();
con.Close();
Session["Reservation_id"] = TextBox6.Text;Response.Redirect("reservation.aspx");
}
}Thursday, December 17, 2009 9:18 AM -
User-1758998371 posted
I am using more than one query in the same on click bottom <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Selecting and updating and inserting commands<o:p></o:p>
Now I am getting this error I am inserting numbers, text <o:p></o:p>
<o:p> </o:p>
Dose it needs to be converted first before insert it to the database?<o:p></o:p>
Also<o:p></o:p>
The con, cmd and ds, ad should it be different in each and every query or can I use the same?!<o:p></o:p>
I know I poor in c# t hat why I will ask even the simple questions<o:p></o:p>
<o:p> </o:p>
Please someone help me I need to submit it day after tomorrow <o:p></o:p>
<o:p> </o:p>
I need your help <o:p></o:p>
<o:p> </o:p>
Thursday, December 17, 2009 2:15 PM -
User3866881 posted
You cannot use something like this:
SqlCommand cmd,cmd1,cmd2,cmd3,cmd4,cmd5 = new SqlCommand();
This means only the last cmd5 will be initialized, from cmd1 to cmd4 will still be null.
You'll have to initialize them one by one, just like
SqlCommand cmd1 = new SqlCommand();
SqlCommand cmd2 = new SqlCommand();
SqlCommand cmd3 = new SqlCommand();
......
Sunday, December 20, 2009 9:14 PM -
User-1758998371 posted
it solved
i get some codes from my class mate
and i modify it
i delivered my project before three hours it was good the teachers love it
it dose not show any error Wink
thanks for help Laughing- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, December 22, 2009 6:29 AM -
User795661788 posted
ALRIYAMI , try to brush up your skills , the problem is not at all about the logic , its all about the basics, plz go through this site , it will help you a lot .
http://www.4guysfromrolla.com/
Regards,
Jaipal.
Tuesday, December 22, 2009 8:01 AM