hey i am designing a Registration Page. In that i am having 3 comboboxes, one for country,one for state & last one for city. I am able to display all countries, states & cities entries from database. Here is my code. 


SqlDataAdapter da = new SqlDataAdapter("select * from Country","Data Source=.;Initial Catalog=master;Integrated Security=True;");
            da.Fill(ds);
             int a =ds.Tables[0].Rows.Count;

             for (int j = 0; j < a; j++)
             { comboBox1.Items.Add(ds.Tables[0].Rows[j][1]);
             
             }

             SqlDataAdapter da1 = new SqlDataAdapter("select * from City", "Data Source=.;Initial Catalog=master;Integrated Security=True;");
             da1.Fill(ds1);
             int i = ds1.Tables[0].Rows.Count;

             for (int s = 0; s < i; s++)
             {
                 comboBox2.Items.Add(ds1.Tables[0].Rows[s][1]);

             }        
        }

[U][B]Now i want assistance on this, suppose a User select a Particular Country,say India then in the state combobox it displays States of India only & on selecting the State it displays Cities of that state only.
[/B][/U]

Thanks in Advance. :)

write this code in comboBox1_click event.

SqlDataAdapter da1 = new SqlDataAdapter("select City_name from City where country_name='"+Combobox1.Text+"'", "Data Source=.;Initial Catalog=master;Integrated Security=True;");
da1.Fill(ds1);
int i = ds1.Tables[0].Rows.Count;

for (int s = 0; s < i; s++)
   {
   comboBox2.Items.Add(ds1.Tables[0].Rows[s][1]);

     }
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlDataAdapter da1 = new SqlDataAdapter("select City_name from City where country_name='" + Combobox1.SelectedText + "'", "Data Source=.;Initial Catalog=master;Integrated Security=True;");
            da1.Fill(ds1);
            int i = ds1.Tables[0].Rows.Count;

            for (int s = 0; s < i; s++)
            {
                comboBox2.Items.Add(ds1.Tables[0].Rows[s][1]);

            }
        }

Use, this for comboBox1 Selected Index Changed Event.
and Write the Similar Code to Fill the Details after Selecting a State in comboBox2 also.

Hope, this Helps.

Get the states based on selected country to populate comboBox2:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlDataAdapter da1 = new SqlDataAdapter("select State from States where country_name='" + combobox1.SelectedItem.ToString() + "'", "Data Source=.;Initial Catalog=master;Integrated Security=True;");
            da1.Fill(ds1);
            int i = ds1.Tables[1].Rows.Count;

            for (int s = 0; s < i; s++)
            {
                comboBox2.Items.Add(ds1.Tables[1].Rows[s][1]);
            }
        }

Then you geta all cities based on selected state:

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlDataAdapter da1 = new SqlDataAdapter("select City_name from Cities where State_Name ='" + combobox2.Selecteditem.ToString() + "'", "Data Source=.;Initial Catalog=master;Integrated Security=True;");
            da1.Fill(ds1);
            int i = ds1.Tables[2].Rows.Count;

            for (int s = 0; s < i; s++)
            {
                comboBox3.Items.Add(ds1.Tables[2].Rows[s][1]);
            }
        }

Table for index 0 is for the Countries, table at index 1 is for the states, and table at index 2 is for the cities (table[0], table[1], table[2]).

<code>

thanks a lot guys for your solutions!!

</code>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.