Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,
I am design One windows Form Application. I am Taken 3 Combo Boxes.First Box Display Country names.Then Select one item display selected country states in second combo box.But I am Select State Then Not Display city's in third combo box.i am using Sql Server database. country names taken as table name.state name as column name,city name as fields.
so Please Give Me Some Idea To Solve This Problem.
Posted
Updated 8-Feb-22 18:23pm
Comments
gqadirshabbir 4-May-12 2:11am    
share your code that will clear where the correction is needed ...

The design specified in the question looks like as follows:
1. One table each for each Country.
2. One Column each of each State in each country table.
3. One Field (Row) for each City in each state.

This design is very difficult to implement as any change in No. of Countries requires a change in the No. of Tables in the Database. Similarly any change in the No. of States requires change in the No. of Columns of the corresponding Country table.

A better option is to create three Tables Country, State, City which will store one data item per row for Country, State and City respectively.

Then for the functionality stated in the question, the procedure as detailed in the following answer may be used for Windows Forms Application stated in the question.
3 combobox for country,state and cities.when i select one country that coressponing states will display in 2nd combobox,and when i select one state that coressponing cities will display in...[^]
 
Share this answer
 
v2
Comments
Sandeep Mewara 4-May-12 10:03am    
Good suggestion. 5!
VJ Reddy 4-May-12 10:16am    
Thank you, Sandeep.
Shahin Khorshidnia 4-May-12 22:22pm    
Good solution VJ.
+5
VJ Reddy 4-May-12 23:38pm    
Thank you, Shahin.
hi firstly check for the event of state combobox it must be firing an event.
if if it firing an event then you can write your code in that event as follows

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("provide connection string here");
DataSet ds = new DataSet();
SqlDataAdapter da=new SqlDataAdapter("select " + state + " from " + country, conn);
da.Fill(ds);
ds.Tables[0]; this will have one column which has all the data of cities for the state and country you've selected
}
when you are done with this you can bind this ds.tables[0] with your comboBox.
Note:- state and country here are string variables,and they are selected values of respective dropdowns
Mark your answer as solved if you've got the answer
thanks.
 
Share this answer
 
v2
This earlier question[^] may help you.
 
Share this answer
 
XML
<ComboBox Grid.Column="8" Grid.Row="8" Height="23" HorizontalAlignment="Left" Name="comboBox2" VerticalAlignment="Center" Width="180">
                <ComboBoxItem Content="Days" />
                <ComboBoxItem Content="Weeks" />
                <ComboBoxItem Content="Months" />
                <ComboBoxItem Content="Years" />
            </ComboBox>
 
Share this answer
 
simple example

make 2 tables
first for countries
like this
CountryId CountryName
1 Palestine
2 Germany
3 .......

the second for states
CountryId StateId StateName CiteisName
1 1 Jerusalem abo dees-selawan-shoafat-bait hanena-...
1 2 Gazza gazza-khan yones-rafah-der albalh-...
1 3 Tulkarm Illar-Attil-alnazleh-....
*********************************************************************************
1- first fill the ComboCountries (by code or insert it manualy)
2- when you choose any contry search in the states table like this :
select StateName form states where CountryId = " & CountryId & " oeder by StateName
and fill the combo of states with result
3- when you choose any State search in the states table like this
select CiteisName form states where StateName = '" & compo3 & "' oeder by StateName
4-
CompoCiteis.clear
Dim CityArry() As String
CityArry() = Split(rs.Fields(0), "-")
For i = 0 To UBound(CityArry)
CompoCiteis.additem CityArry(i)
Next

i hope this help
 
Share this answer
 
Comments
[no name] 23-Nov-16 11:47am    
The question was asked and answered FOUR years ago
The question is clearly marked C# your code is VB
Your code is not formatted
Your code would not compile even if if were C#
Your queries would not work anyway.
Glaring spelling and grammar errors detract from your solution
Having just joined today, maybe you should learn how the web site works before resurrecting ancient already answered questions.
Now run the applicationusing System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace CountryStateCity
{
public partialclass WebForm1 : System.Web.UI.Page
{
private SqlConnection conn = new SqlConnection("Data source=.; uid=sa; pwd=Password$2; database=CountryStateCity");

public void Bind_ddlCountry()
{
conn.Open();
SqlCommand cmd =new SqlCommand("select County,CountryId from Country", conn);
SqlDataReader dr = cmd.ExecuteReader();
ddlcountry.DataSource = dr;
ddlcountry.Items.Clear();
ddlcountry.Items.Add("--Please Select country--");
ddlcountry.DataTextField = "County";
ddlcountry.DataValueField = "CountryId";
ddlcountry.DataBind();
conn.Close();
}
public void Bind_ddlState()
{
conn.Open();

SqlCommand cmd =new SqlCommand("select State,StateID from countryState where CountryId='" + ddlcountry.SelectedValue +"'", conn);

SqlDataReader dr = cmd.ExecuteReader();
ddlstate.DataSource = dr;
ddlstate.Items.Clear();
ddlstate.Items.Add("--Please Select state--");
ddlstate.DataTextField = "State";
ddlstate.DataValueField = "StateID";
ddlstate.DataBind();
conn.Close();
}
public void Bind_ddlCity()
{
conn.Open();
SqlCommand cmd =new SqlCommand("select * from stateCity where StateId ='" + ddlstate.SelectedValue +"'", conn);

SqlDataReader dr = cmd.ExecuteReader();
ddlcity.DataSource = dr;
ddlcity.Items.Clear();
ddlcity.Items.Add("--Please Select city--");
ddlcity.DataTextField = "City";
ddlcity.DataValueField = "CityID";
ddlcity.DataBind();
conn.Close();
}
protectedvoid Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind_ddlCountry();
}
}
protectedvoid ddlcountry_SelectedIndexChanged(object sender, EventArgs e)
{
Bind_ddlState();
}
protectedvoid ddlstate_SelectedIndexChanged(object sender, EventArgs e)
{
Bind_ddlCity();
}
}
}
Now run the application
 
Share this answer
 
Comments
Shloka Patel 9-Feb-22 0:25am    
I have an ddlcountry and all eror and i have used combobox
Graeme_Grant 9-Feb-22 1:29am    
You do realize that you answered a 10-year-old question? Please focus on current questions...

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