Click here to Skip to main content
15,939,853 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 dropdownlists-
XML
<asp:DropDownList ID="stateList" runat="server" Width ="170px"
                CssClass ="text" AutoPostBack="True"  OnSelectedIndexChanged ="stateList_SelectedIndexChanged">
            <asp:ListItem Value ="Andhra Pradesh">Andhra Pradesh</asp:ListItem>
            <asp:ListItem Value ="Arunachal Pradesh">Arunachal Pradesh</asp:ListItem>
<asp:ListItem Value ="Assam">Assam</asp:ListItem>
<asp:ListItem Value="Bihar">Bihar</asp:ListItem>
<asp:ListItem Value ="Chattisgarh">Chhattisgarh</asp:ListItem>
<asp:ListItem Value ="Goa">Goa</asp:ListItem>
<asp:ListItem Value="Gujarat">Gujarat</asp:ListItem>
<asp:ListItem Value ="Haryana">Haryana</asp:ListItem>

</asp:DropDownList>
XML
<asp:DropDownList ID="cityList" runat="server" Width ="135px" CssClass ="text" >
            </asp:DropDownList>


State and City values are there in the database. In the Page_Load the cityList needs to be populated and the specified city needs to be selected. Now, the cityList is being populated ,but when i give
SQL
cityList .Items .FindByText( cityname).Selected =true ;

it returns object reference not set to an instance of an object. What is wrong with this code?
I have given this code inside
SQL
if(!IsPostBack )
   {}

Thanks in advance

Nothing is selected by default. I have populated the cityList in the following way
MIDL
cityList.Items.Add("Agra");
            cityList.Items.Add("Aligarh");
Posted
Updated 28-Jul-10 19:36pm
v2
Comments
Sandeep Mewara 29-Jul-10 1:52am    
So, did you tried other parts of my answer? Debugged it? Checked what is NULL?
Soumini Ramakrishnan 29-Jul-10 2:27am    
Thanks. I corrected it. the cityList hadn't the particular city name

Did you tried to use DEBBUGER? Put a break point on this line and check what comes to be NULL. Object reference error happens when you try to access some property of a null object.

Might be: cityList.Items.FindByText( cityname) is NULL and you keep on trying a Selected property of it.
Further, this line of code is if not postback => page load for first time. Is anything selected by default when page loads? I doubt, so most probably thats the reason of not getting any value as selected and thus an error.
 
Share this answer
 
cityList.ClearSelection();
cityList .Items .FindByText( cityname).Selected =true ;

try this hope this will work

thanks,

amit
 
Share this answer
 

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