Click here to Skip to main content
15,903,385 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
int count = Convert.ToInt16(ddlvehicles.SelectedValue);
           for (int i = 2; i <= count; i++)
           {
               cmd.Parameters.Clear();
               cmd.CommandText = "usp_booking_insert";
               cmd.CommandType = CommandType.StoredProcedure;
               cmd.Parameters.AddWithValue("@user_id", ddluname.SelectedValue);
               cmd.Parameters.AddWithValue("@journey_place", txtjplace.Text);
               cmd.Parameters.AddWithValue("@pickup_date", txtpickup_daterri.Text);
               cmd.Parameters.AddWithValue("@pickup_address", txtpickup_address.Text);
               cmd.Parameters.AddWithValue("@return_date", txtreturndate.Text);
               cmd.Parameters.AddWithValue("@return_address", txtreturn_address.Text);
               cmd.Parameters.AddWithValue("@service_id", ddlsname.SelectedValue);
               cmd.Parameters.AddWithValue("@vehicle_id", ddlvehicles.SelectedValue);
               cmd.Parameters.AddWithValue("@vtype_ac_nonac", ddlvtype1.SelectedValue);
               cmd.Parameters.AddWithValue("@vrate_km_day", ddlrate1.SelectedValue);
               cmd.Parameters.AddWithValue("@mode_of_payment", rdbtnpayment.SelectedItem.ToString());
               int vid = Convert.ToInt16(((DropDownList)mvbooking.Views[0].FindControl("ddlvehicles1" + i+1)).SelectedValue);
               string vtype = ((DropDownList)mvbooking.Views[0].FindControl("ddlvtype1" + i+1)).SelectedValue;
               string vrate = ((DropDownList)mvbooking.Views[0].FindControl("ddlvrate1" + i+1)).SelectedValue;



dis is my insert query. bt i want to add vehicle upto they are selected. i tried using for loop like dis.. but it is not working there is an error of "Object reference not set to an instance of an object." in "int vid=...;" line

help me plz
Posted
Comments
ZurdoDev 19-Mar-13 13:57pm    
So, you need to find out how to get access to ddlvehicles1i.
mhd.sbt 19-Mar-13 13:58pm    
hi buddy
can i see your complete code?
i mean,asax file that related to this page
and page or class that contain this page
Jegan Thiyagesan 19-Mar-13 15:46pm    
Are you sure you should be looking for control ("ddlvehicles1" + i+1)?
shouldn't this be ("ddlvehicles" + i+1), without the number at the end, because you are adding the number using the for loop.
if i = 0;
("ddlvehicles1" + i+1) = "ddlvehicles11" and
("ddlvehicles" + i+1) = "ddlvehicles1".
Member 9671810 20-Mar-13 12:15pm    
yup..
but the thing was that i was not knowing how to start my loop as my ddl values stares from 2 or more..
i mean when i used dis loop to display panel it worked so hoped that this will also work..
and i have ddlvehicle1 upto ddlvehicle10.
but anyways i solved..

"Thanx for your co-operation"

C#
int count = Convert.ToInt16(ddlvehicles.SelectedValue);
           for (int i = 1; i <= count; i++)
           {
               cmd.Parameters.Clear();
               cmd.CommandText = "usp_booking_insert";
               cmd.CommandType = CommandType.StoredProcedure;
               cmd.Parameters.AddWithValue("@user_id", ddluname.SelectedValue);
               cmd.Parameters.AddWithValue("@journey_place", txtjplace.Text);
               cmd.Parameters.AddWithValue("@pickup_date", txtpickup_daterri.Text);
               cmd.Parameters.AddWithValue("@pickup_address", txtpickup_address.Text);
               cmd.Parameters.AddWithValue("@return_date", txtreturndate.Text);
               cmd.Parameters.AddWithValue("@return_address", txtreturn_address.Text);
               cmd.Parameters.AddWithValue("@service_id", ddlsname.SelectedValue);
               cmd.Parameters.AddWithValue("@mode_of_payment", rdbtnpayment.SelectedItem.ToString());
               int vid = Convert.ToInt16(((DropDownList)mvbooking.Views[0].FindControl("ddlvnameri" + i)).SelectedValue);
               string vtype = ((DropDownList)mvbooking.Views[0].FindControl("ddlvtype" + i)).SelectedValue;
               string vrate = ((DropDownList)mvbooking.Views[0].FindControl("ddlrate" + i)).SelectedValue;
               cmd.Parameters.AddWithValue("@vehicle_id",vid);
               cmd.Parameters.AddWithValue("@vtype_ac_nonac", vtype);
               cmd.Parameters.AddWithValue("@vrate_km_day", vrate);
           }




Thanx everyone for your co-operation.. but i solved it
 
Share this answer
 
First of all, read this: 3 common causes of system nullreferenceexception (object reference not set to an instance of an object)[^]

Second, if you use:
C#
int count = Convert.ToInt16(ddlvehicles.SelectedValue);

how can you excepect that below code will execute correctly, especially when i is bigger then count?
C#
string vtype = ((DropDownList)mvbooking.Views[0].FindControl("ddlvtype1" + i+1)).SelectedValue;


Third, you want to convert i + 1 to string expression. Try to use: Int32.ToString Method[^] or Convert.ToString method[^].
 
Share this answer
 
Comments
fjdiewornncalwe 19-Mar-13 15:47pm    
5.
Maciej Los 19-Mar-13 15:51pm    
Thank you, Marcus ;)
Jegan Thiyagesan 20-Mar-13 8:06am    
FindControl method takes string as argument, so the ("ddlvtype1" + i+1) automatically converted to string. The addition of i +1 not the type int in place of string are not the problem. The problem is I think is that "ddlvtype1" should not have the "1" at the end. Adding a number to the variable will make the control names start from 11, i.e. if i = 0, ("ddlvtype1" + i+1) = "ddlvtype11" not "ddlvtype1" what it should have been.
Maciej Los 20-Mar-13 10:13am    
About FindControl:
For(){} loop starts with: ddlvtype13, because compiler "see" it as: "ddlvtype1" + Convert.ToString(2 + 1). Maybe it's not problem, but illustrate programmer's intention.
About "1" at the end:
Yes, it could be the reson of troubles, but it could be intentional...

Thank you for suggestions ;)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900