Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void dpdCourse_SelectedIndexChanged(object sender, EventArgs e)
   {
       string name = dpdCourse.SelectedItem.Value;
       var crs = DB.Courses.Where(c => c.Name == name).First();
       crsCode.Text = crs.Id.ToString();

   }


i wrote this code for a dropdown list in a page that is derived from a master page when i debug this page when arrive at the beginning this event it goes to page_load of master page and get this error:

Input string was not in a correct format.

thank you.
Posted
Updated 30-May-12 4:12am
v2
Comments
taha bahraminezhad Jooneghani 30-May-12 10:35am    
use .FirstOrDefault() instead of .First() because if the query is null it returns an empty value not null.
Bryian Tan 30-May-12 10:52am    
which line in the page_load event of the master page throw the error? Please show us the relevant code.
parisa heidari 30-May-12 11:02am    
hi Bryian , i dont have any code in page_load event of the master page it just from the beginnig of event goes at the beginning of page_load event of the master page and throw the error

thanks

There are a couple things going on here, I believe. First, this event is firing when you first load the page. This is normal. However, your value for the drop down is not yet set, so you get an error. The second issue is that you are trying to fire this event right away, but I don't believe that will happen because this code is server-side. Unless you do a POST, you won't see the event fire. Instead, you need to use something like jQuery to handle your events.

For example, in jQuery, you could write the following section:

JavaScript
$("#dropdownID").change(function () {
   <fire your event here>
});


Just make sure you put that inside the DOM ready event so it doesn't fire prematurely.

To learn more about the jQuery change event and to see a working example that will look a lot like your HTML code, look here: http://api.jquery.com/change/[^]

Please note that this has nothing to do with ASP.NET/C# code. In fact, if you implement this method, you should remove your event entirely from your code. Since that event will only fire after a server action (page load, POST, GET), it won't do what you want anyway.
 
Share this answer
 
v3
Comments
parisa heidari 30-May-12 10:39am    
Excuse me , but i don't know jQuery i just start lerning asp.
the value is set with this code:
foreach (Course crs in DB.Courses)
{
ListItem item = new ListItem(crs.ToString());
dpdCourse.Items.Add(item);
}
Tim Corey 30-May-12 10:58am    
I updated my response to give you a link that shows how to use the jQuery code. You really cannot do what you want with C# code. Unlike a desktop application, server-side events don't fire when something happens. They fire when a server action happens (like a POST). The worlds of server-side and client-side are seperated. You need to think through which end the action needs to happen on. Since the user is chaning the dropdown choice, the event needs to fire on the user's end (which is why you use jQuery).
Hi,

I thing, it is due to the casting error. crs.Id.ToString() is throwing the error. May be crs will be null. If it is null, crs.Id.ToString() will through error. Use Convert.ToString(crs.Id). This can handle null exception.
 
Share this answer
 
Comments
Tim Corey 30-May-12 10:53am    
That will probably solve the error but the event still won't fire like the poster wants it to. Server-side code cannot perform client-side actions.
Linto Leo Tom 30-May-12 10:56am    
Check whether autopostback property of the dropdownlist is true.
If not, set it to true.
parisa heidari 30-May-12 10:57am    
thanks Linto Leo Tom but it is stoped at beginnig of event and it don't arrive this line additionally crs is not null but i used your suggestion.
taha bahraminezhad Jooneghani 30-May-12 15:59pm    
if its null , .ToString() throw "not instance of an object" error, not "Input string was not in a correct format".

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