Hi rickabbam,
Check this example. Now please take its reference and correct your code.
Database
I have made use of the following tables with the schema as follows.
Country
State
City
You can download the database table SQL by clicking the download link below.
Download
Form Design
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
private void Form1_Load(object sender, EventArgs e)
{
cmbState.Enabled = false;
cmbCity.Enabled = false;
string strQuery = "SELECT CountryId, CountryName FROM Countries";
cmbCountry.ValueMember = "CountryId";
cmbCountry.DisplayMember = "CountryName";
cmbCountry.DataSource = GetData(strQuery, null).Tables[0];
}
private void cmbCountry_SelectedIndexChanged(object sender, EventArgs e)
{
string strQuery = "SELECT StateId, StateName FROM States WHERE CountryID = @CountryID";
SqlParameter[] sqlParameter = new SqlParameter[1];
sqlParameter[0] = new SqlParameter("@CountryID", cmbCountry.SelectedValue);
cmbState.Enabled = true;
cmbState.DisplayMember = "StateName";
cmbState.ValueMember = "StateId";
cmbState.DataSource = this.GetData(strQuery, sqlParameter).Tables[0];
}
private void cmbState_SelectedIndexChanged(object sender, EventArgs e)
{
string strQuery = "SELECT CityId, CityName FROM Cities WHERE StateId = @StateId";
SqlParameter[] sqlParameter = new SqlParameter[1];
sqlParameter[0] = new SqlParameter("@StateId", cmbState.SelectedValue);
cmbCity.Enabled = true;
cmbCity.DisplayMember = "CityName";
cmbCity.ValueMember = "CityId";
cmbCity.DataSource = this.GetData(strQuery, sqlParameter).Tables[0];
}
private DataSet GetData(string query, SqlParameter[] sqlParameter)
{
string strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(query, con);
if (sqlParameter != null)
{
cmd.Parameters.AddRange(sqlParameter);
}
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds;
}
VB.Net
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
cmbState.Enabled = False
cmbCity.Enabled = False
Dim strQuery As String = "SELECT CountryId, CountryName FROM Countries"
cmbCountry.ValueMember = "CountryId"
cmbCountry.DisplayMember = "CountryName"
cmbCountry.DataSource = GetData(strQuery, Nothing).Tables(0)
End Sub
Private Sub cmbCountry_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim strQuery As String = "SELECT StateId, StateName FROM States WHERE CountryID = @CountryID"
Dim sqlParameter As SqlParameter() = New SqlParameter(0) {}
sqlParameter(0) = New SqlParameter("@CountryID", cmbCountry.SelectedValue)
cmbState.Enabled = True
cmbState.DisplayMember = "StateName"
cmbState.ValueMember = "StateId"
cmbState.DataSource = Me.GetData(strQuery, sqlParameter).Tables(0)
End Sub
Private Sub cmbState_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim strQuery As String = "SELECT CityId, CityName FROM Cities WHERE StateId = @StateId"
Dim sqlParameter As SqlParameter() = New SqlParameter(0) {}
sqlParameter(0) = New SqlParameter("@StateId", cmbState.SelectedValue)
cmbCity.Enabled = True
cmbCity.DisplayMember = "CityName"
cmbCity.ValueMember = "CityId"
cmbCity.DataSource = Me.GetData(strQuery, sqlParameter).Tables(0)
End Sub
Private Function GetData(ByVal query As String, ByVal sqlParameter As SqlParameter()) As DataSet
Dim strConnString As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
Dim con As SqlConnection = New SqlConnection(strConnString)
Dim ds As DataSet = New DataSet()
Dim cmd As SqlCommand = New SqlCommand(query, con)
If sqlParameter IsNot Nothing Then
cmd.Parameters.AddRange(sqlParameter)
End If
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
da.Fill(ds)
Return ds
End Function
Screenshot