i maintained two drop down list one is for state and country [Resolved]

Posted by Amar888 under Regular Expressions on 3/2/2010 | Views : 8593 | Status : [Member] | Replies : 4
hi,
in my form i have two dropdown list one is for state and another country, my problem is if i select state, automatically the country should display or vice versa any one can know pls help me


thanking u ,




Responses

Posted by: Syedshakeer on: 3/3/2010 [Member] Starter

Up
0
Down

Resolved
Hi Amar,
Hi

First Create a Table with a Name States .It contains two columns as stateid and statename
stateid statename
----------------------
1 Andhra Pradesh
2 Karnataka
3 Maharastra

Next Create Another table with a name citys .It contains stateid and cityname columns.

stateid cityname
-------------------------
1 Hyderabad
2 Banglore
3 Mumbai


Drag and Drop Two Dropdownlist.
In first DropdownList retrive the data from a table states as follows

write in PageLoed Event:
SqlCommand cmd=new SqlCommand("select * from states",conn); 

SqlDataReader reader=cmd.ExecuteReader();
DropDownList1.Items.Cleer();
while(reader.Read())
{
ListItem li=new ListItem();
li.Value=reader["stateid"];
li.Text=reader["statename"];
DropDownList1.Items.Add(li);

}

When you select a State the following Capital citys have to display in second dropdownlist2
so Doubleclik on DropdownList1 and write the below code:

SqlCommand conn=new SqlCommand("your connection"); 

conn2.Open();
SqlCommand cmd2=new SqlCommand("select * from citys where stateid="+ DropdownList1.SelectedValue+",conn2);
SqlDataReader reader2=cmd2.ExecuteReader();
DropDownList2.Items.Clear();
while(reader2.Read())
{
ListItem li2=new ListItem();
li2.Value=reader2["stateid"];
li2.Text=reader2["cityname"];
DropDownList2.Items.Add(li2);

}


Syed Shakeer Hussain

Amar888, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 3/2/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
I think I did the same thing in my website using one user control.

Just check it:
http://www.abhisheksur.com/2010/01/how-to-expose-events-from-usercontrol.html

Cheers.

www.abhisheksur.com

Amar888, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Amar888 on: 3/3/2010 [Member] Starter

Up
0
Down
its not working


CS0266: Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)


this is the error

Amar888, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 3/3/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
Use

while(reader.Read())

{

ListItem li=new ListItem();

li.Value=reader["stateid"] as string;

li.Text=reader["statename"] as string;

DropDownList1.Items.Add(li);



}

Instead of writing it directly.

:)

www.abhisheksur.com

Amar888, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response