Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET C#4.0
i want to add seat numbers selected by a single user into the database.
void BindSeats()
   {
       Label14.Text = chargePerSheet.ToString();
       string sel = String.Join(",", (from b in objEntities.BookingDetails
                                      join s in objEntities.SeatDetails on b.BookingID equals s.BookingID
                                      where b.TourID == tourID
                                      select s.SeatID).ToArray());
       ClientScript.RegisterStartupScript(this.GetType(), "Init", "init([" + sel + "]);", true);
 
   }
 
getting an error like 'object reference not set' the code is like:
 
protected void btnSubmit_Click(object sender, EventArgs e)
   {
       paymentmethod();
       sendMail();
 
       StarBusModel.BookingDetail objBooking = new StarBusModel.BookingDetail();
       objBooking.TourID = tourID;
       objBooking.Name = Label11.Text;
       objBooking.Phone = TextBox2.Text;
       objBooking.Amount = Convert.ToDecimal(Request.Form[Label14.ClientID]);
       string[] seats = Request.Form[Label13.ClientID].Split(new char[] { ',' });
       for (int i = 0; i < seats.Length; i++)
           objBooking.SeatDetails.Add(new StarBusModel.SeatDetail() { SeatID = Convert.ToInt32(seats[i]) });
       objEntities.BookingDetails.AddObject(objBooking);
       objEntities.SaveChanges();
       BindSeats();
 

 

       Response.Redirect("thankyou.aspx");
Posted 1-May-13 7:24am
Edited 1-May-13 21:55pm
v3
Comments
richcb at 1-May-13 12:25pm
   
You need to debug and see where the exception is being thrown. Once you have done that, it will be clear that you are using an object that you did not instantiate.
degrader404 at 1-May-13 12:34pm
   
actually i want to add two distinct numbers in my database that holds values for tickets selected
richcb at 1-May-13 12:35pm
   
See my solution, it is what you need.
degrader404 at 1-May-13 12:45pm
   
still getting the same error:Object reference not set to an instance of an object.
richcb at 1-May-13 12:47pm
   
Debug again and see where it is coming from now. My guess it is coming from "objEntities". You never instantiate that object either.
degrader404 at 1-May-13 13:07pm
   
it shows the same.can you give me your email-id so that i can send you all of my code.then you can go through it. plzzz
richcb at 1-May-13 13:58pm
   
Just use the "Improve question" widget and add what you have now below what is already in the thread. Show them both.
degrader404 at 2-May-13 2:47am
   
now i am getting a new error:
Input string was not in a correct format.
Line 136: objBooking.SeatDetails.Add(new StarBusModel.SeatDetail() { SeatID = Convert.ToInt32(seats[i]) });
HariPrasad katakam at 1-May-13 12:26pm
   
In which line you are getting the error? and also share the full error message
degrader404 at 1-May-13 12:41pm
   
at this line
string[] seats = Request.Form[Label13.ClientID].Split(new char[] { ',' });
degrader404 at 1-May-13 12:46pm
   
this is the error
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.
HariPrasad katakam at 1-May-13 12:51pm
   
Are you sure that you have a Label with ID Label13 in your webpage? and is it stand alone page or having master page?
degrader404 at 1-May-13 12:53pm
   
Yes i have it.It has a master page.
degrader404 at 1-May-13 13:00pm
   
It has a label with that id and a master page.
HariPrasad katakam at 1-May-13 13:13pm
   
replace the Line
string[] seats = Request.Form[Label13.ClientID].Split(new char[] { ',' });
 
With
 
string[] seats = Label13.Text.Split(new char[] { ',' });
degrader404 at 2-May-13 2:32am
   
i did what you said.but now it is showing anew error on this line:
objBooking.SeatDetails.Add(new StarBusModel.SeatDetail() { SeatID = Convert.ToInt32(seats[i]) });
HariPrasad katakam at 2-May-13 3:25am
   
What is the value you have in Label? i mean Label13.Text value? As per your logic it should be comma separated numeric.. Ex: 100,101,102 is That correct? if it is not the error message could be "Format exception was unhandled, Input string was not in a correct format.".
degrader404 at 2-May-13 12:02pm
   
yes your correct.the error message is 'Input string was not in a correct format.'
HariPrasad katakam at 2-May-13 3:43am
   
And also try to replace the line
 
objBooking.SeatDetails.Add(new StarBusModel.SeatDetail() { SeatID = Convert.ToInt32(seats[i]) });
 
with
 
objBooking.SeatDetails.Add(new StarBusModel.SeatDetail() { SeatID =int.TryParse(seats[i]) });
degrader404 at 2-May-13 12:35pm
   
i replaced it but now it shows:
Error 1 No overload for method 'TryParse' takes 1 arguments D:\WebSite4\payment.aspx.cs 140 77 D:\WebSite4\
HariPrasad katakam at 3-May-13 6:12am
   
Sorry Was in hurry, below is one alternative i can suggest. might not be a effective way of coding,but until you find some other solution you can use this
 
replace below lines of code
 
string[] seats = Request.Form[Label13.ClientID].Split(new char[] { ',' });
for (int i = 0; i < seats.Length; i++)
objBooking.SeatDetails.Add(new StarBusModel.SeatDetail() { SeatID = Convert.ToInt32(seats[i]) });
 
with
 

string[] seats = Request.Form[Label13.ClientID].Split(new char[] { ',' });
int temp;
for (int i = 0; i < seats.Length; i++)
if(int.TryParse(seats[i],out temp))
objBooking.SeatDetails.Add(new StarBusModel.SeatDetail() { SeatID = Convert.ToInt32(seats[i]) });
 
But I am suspecting your label(which is storing all the selected seats with comma separated) may contain other than int values. please check that front as well.
degrader404 at 3-May-13 12:57pm
   
thanks for your time.but now its showing an error:
The specified cast from a materialized 'System.String' type to the 'System.Int32' type is not valid.
at
string sel = String.Join(",", (from b in objEntities.BookingDetails
join s in objEntities.SeatDetails on b.BookingID equals s.BookingID
where b.TourID == tourID
select s.SeatID).ToArray());
degrader404 at 3-May-13 13:05pm
   
can i mail you all of my code
HariPrasad katakam at 3-May-13 13:19pm
   
Ok do that.send it to urstrulyhariprasad@gmail.com. but it may take some time to give you reply.
degrader404 at 4-May-13 11:23am
   
i have send the email.plz check it.thanks man.
degrader404 at 8-May-13 2:01am
   
hey did you get the solution ?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It is stemming from "string[] seats = """. You never instantiated a string array. It should look like this:
string[] seats = new string[1];
Then you can assign it the value you have.
  Permalink  
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 519
2 Maciej Los 305
3 BillWoodruff 250
4 Mathew Soji 195
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,777
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 2 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100