hey can u help me out with the below problem

The problem i that when i click on country i should be able to select the corresponding states...i have done this in asp.net and it works fine but here when i try to select the country here nothing happens and the code automitically goes to the selected indexchanged event even if didnt selected nething...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        SqlConnection con = new SqlConnection("Data Source=SNSS1\\SQLEXPRESS;Initial Catalog=Employee;User ID=sa;Password=eLog!234");

        DataTable dt = new DataTable();
        DataSet ds = new DataSet();
        public Form2()
        {
            InitializeComponent();
            Form1 frm = new Form1();
            frm.Hide();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            FillCountry();
        }

        private void cmb_country_SelectedIndexChanged(object sender, EventArgs e)
        {

            Int32 CountryId = Convert.ToInt32(cmb_country.SelectedValue);
            FillStates(CountryId);


        }

        private void cmb_state_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void cmb_city_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        public void FillCountry()
        {
            con.Open();

            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Tbl_CountryStateCity WHERE ParentID=1", con);
            da.Fill(ds, "s"); ;
            dt = ds.Tables[0];

            if (ds.Tables[0].Rows.Count > 0)
            {
               
                cmb_country.DisplayMember = "Name";
                cmb_country.ValueMember = "ID";
                cmb_country.DataSource = dt;
               
                


            }
        }
        public void FillStates(Int32 CountryId)
        {

            SqlCommand cmd = new SqlCommand("SELECT * FROM Tbl_CountryStateCity WHERE ParentID=" + CountryId + "", con);
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds, "s"); ;
            dt = ds.Tables[0];
            if (ds.Tables[0].Rows.Count > 0)
            {
                cmb_state.DisplayMember = "Name";
                cmb_state.ValueMember = "ID";
                cmb_state.BindingContext = new BindingContext();
                cmb_state.DataSource = dt;

            }

        }
    }
}

i even tried placing cmb_state.datasource=dt above Displaymember

Plzz helpme

Hi,

I have tried with the above code. It is working fine and list out the states.

Did you get any errors?

some points to verify like,

1) check the field names used in cmb assigning
2) check the selected index value
3) check the data table values.


if you found please let us know.

This article has been dead for over six months. Start a new discussion instead.