Respected Users,
I have two drop downs in my applications as ddlCountry and ddlState.
I have database as
tlbCountry
ID(PK) | countryName
tlbState
ID(PK) | countryName | stateName
On loading of page I have loaded all the items in ddlCountry dropdown as
sqldataadapter da=new sqldataadapter("select countryName from tlbCountry",con);
dataset ds=new dataset();
da.fill(ds);
for(int i=0;i<ds.tables[0].rows.count;i++)
ddlCountry.items.add(ds.Tables[0].Rows[i][0].toString());
When page gets loaded, upto this its working fine.
But,
When on Textchange or selection change event of ddlCountry, it makes attempt to take the values of corresponding states from tlbStates table as follows. Its not working, I have done that as follows,
sqldataadapter da=new sqldataadapter("select stateName from tlbState where countryName like '"+ddlCountry.selectedItem.toString()+"'",con);
dataset ds=new dataset();
da.fill(ds);
for(int i=0;i<ds.tables[0].rows.count;i++)
ddlState.items.add(ds.Tables[0].Rows[i][0].toString());
In this case its not loading the ddlState drop down.
When I turn on autopostback for ddlCountry, it again reloads the page with no values in ddlState.
What can be the issue?
Note:- I have used AJAX UPDATE PANEL.
Change your query to this:
Populate your
ddlCountryon Page_Load only when the request is not aPostBack