Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a scenario where I let a user select his state and city by using two cascading dropdown list (one of AJAX Control Toolkit Extenders).
I also have an edit section where he can update the state and city. If he does not have anything at the first place, the database entry is null. In that scenario, I simply show him the cascading drop downs and he selects a valid entry.
Coming to the issue I am facing, the issue is when he actually has entered the data. If he has already saved the data, I have to make sure that the dropdown has those items selected. Now on the page load when the page is not postback, I tried to make a selection. However I get a
C#
NullReferenceException
.
How do I make sure that if an entry does exist for a user, it should bind the dropdowns accordingly?
Posted

I just found the solution for the problem. It can be set on the extender itself. The extender has a property called
C#
SelectedValue 
which is just perfect to solve this scenario.
 
Share this answer
 
Object reference not set to an instance of an object

This error happens when you try to use a property or call a method of an object that is null. More details: here[^]

A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line. Check the objects of that line and see if any one is null and you are trying to use that objects property. Handle the same.

Since you have not shared the code, it's difficult to point possible places but the fix would be to handle the scenario such that whenever there is data for person in Db, fetch it and set it from the default selection list.

Steps:
1. Populate the State dropdown.
2. Set the selected value of dropdown based on the DB selection value for that user
3. Get the cities of the state based on the selection
4. populate the cities dropdown based on the cities fetched
5. Set the seletced value of dropdown based on the DB selected value for that user

Try!
 
Share this answer
 
Comments
Pankaj Nikam 10-Jun-12 5:50am    
Thanks... Just found the solution to the code. The solution you mentioned works well if they were plain drop down lists (where I could debug everything). But in my case, its using a cascading dropdown extension of the AJAX Control toolkit. I was having a tough time till I found the property actually in the extender itself.
Thanks for your help though :)

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