R.D Ip

R.D Ip

  • NA
  • 76
  • 8.3k

how to fetch statename based on country

Feb 27 2021 5:33 AM
I   am working on windows form application c sharp
I am trying to fetch statename based on country in combobox
now I am stuck and my logic is not work,which place need to correction
first I fetch a country name in combobox successfully
now I am trying to fetch statename based on country but my logic is not work
 
sp : load country 
 
 
sp  : load state based on country
 
 
code: 
  1.  private void Form1_Load_1(object sender, EventArgs e)   //load country successfully in combobox
  2.  {  
  3.      cnnString.Open();  
  4.      SqlCommand cmd = new SqlCommand("sp_LoadCountry", cnnString);  
  5.   
  6.      SqlDataReader reader = cmd.ExecuteReader();  
  7.      DataTable dt = new DataTable();  
  8.      dt.Columns.Add("countryid");  
  9.      dt.Columns.Add("countryname");  
  10.      dt.Load(reader);  
  11.   
  12.      countrycomboBox.ValueMember = "countryid";  
  13.      countrycomboBox.DisplayMember = "countryname";  
  14.      countrycomboBox.DataSource = dt;  
  15.   
  16.      cnnString.Close();  
  17.   
  18.  }  
  19.   
  20.  private void countrycomboBox_SelectedIndexChanged(object sender, EventArgs e)    //here I am stuck state not load based on country
  21.  {  
  22.      SqlCommand cmd = new SqlCommand("sp_LoadStateBaseOnCountry", cnnString);  
  23.      cmd.CommandType = CommandType.StoredProcedure;  
  24.      cmd.Parameters.AddWithValue("countryid",countrycomboBox.Items.IndexOf(cmd));  
  25.      SqlDataAdapter da = new SqlDataAdapter(cmd);  
  26.      da.Fill(dt);  
  27.      statecomboBox.ValueMember = "stateid";  
  28.      statecomboBox.DisplayMember = "statename";  
  29.      statecomboBox.DataSource = dt;
  30.  }  
output:
 
 
now I am stuck in logic need help 

Answers (11)