Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace country_state_city
{
    public partial class Form1 : Form
    {

        SqlConnection con = new SqlConnection(*************);
        
        public Form1()
        {
            InitializeComponent();
           
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
            SqlCommand cmd = new SqlCommand("select * from Country", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            comboBox1.SelectedIndex = -1;
            comboBox1.DataSource = ds.Tables[0];
           
            comboBox1.DisplayMember = "County";
            comboBox1.ValueMember = "Countryid";
           
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //MessageBox.Show(comboBox1.SelectedIndex.ToString());
            if (comboBox1.SelectedIndex >= 0)
            {

                SqlDataAdapter da = new SqlDataAdapter("Select * From CountryState Where CountryId='" + comboBox1.SelectedValue + "'", con);
                DataSet ds = new DataSet();
                da.Fill(ds);

                comboBox2.DataSource = ds.Tables[0];
                comboBox2.DisplayMember = "State";
                comboBox2.ValueMember = "StateId";
            }
            
        }

        

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox2.SelectedIndex > 0)
            {

                SqlDataAdapter da = new SqlDataAdapter("Select * From StateCity Where StateId='" + comboBox2.SelectedValue + "'", con);
                DataSet ds = new DataSet();
                da.Fill(ds);

                comboBox3.DataSource = ds.Tables[0];
                comboBox3.DisplayMember = "City";
                comboBox3.ValueMember = "CityId";
            }
        }
    }
}
Posted
Updated 2-Jul-14 0:23am
v2
Comments
Murugesan22 3-Jul-14 5:35am    
try this, If i am not wrong

Can you please add form load event in if(!ispostback)

Because everytime load form means it give countryid is zero thats why you cant get correct value in state

I will you post same content with ispostback option

Couple of things to consider here ...

1. Your comboBox1_SelectedIndexChanged event is being fired as the comboBox is being populated, and the SelectedIndex equals zero at that point. So processing is dropping into your code, but before there is anything actually selected.

One way around this is to unsubscribe from the comboBox1_SelectedIndexChanged event while you are populating the comboBox. e.g. like this
C#
this.comboBox1.SelectedIndexChanged -= new System.EventHandler(this.comboBox1_SelectedIndexChanged);
SqlCommand cmd = new SqlCommand("select * from Country", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();

Then after you have populated the comboBox, you can subscribe to the event, and force a selection to happen e.g.
C#
comboBox1.SelectedIndex = -1;
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
comboBox1.SelectedIndex = 0;


2. Your comboBox1 is going to populated with numbers because you have used
C#
comboBox1.DisplayMember = "County";
- there is no such column, it should be Country

3. Never use string concatenation to build your sql statements like this
C#
SqlDataAdapter da = new SqlDataAdapter("Select * From CountryState Where CountryId='" + comboBox1.SelectedValue + "'", con);
You leave yourself wide open to SQL Injection attacks[^] - use parameterized queries instead e.g.
C#
SqlCommand cmd = new SqlCommand("Select * from StateCity Where StateId=@id", con);
cmd.Parameters.AddWithValue("@id", comboBox2.SelectedValue);
SqlDataAdapter da = new SqlDataAdapter(cmd);


4. You need to get into the habit of using some best practices such as Try-Catch blocks[^] and using statements[^]
 
Share this answer
 
Hello


See the below code posted by me

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace country_state_city
{
    public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection("*************");
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("select * from Country", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);

            comboBox1.DataSource = ds.Tables[0];

            comboBox1.DisplayMember = "County";
            comboBox1.ValueMember = "Countryid";
            comboBox1.SelectedIndex = 0;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show(comboBox1.SelectedIndex.ToString());
            if (comboBox1.SelectedIndex >= 0)
            {
                SqlDataAdapter da = new SqlDataAdapter("Select * From CountryState Where CountryId='" + comboBox1.SelectedValue + "'", con);
                DataSet ds = new DataSet();
                da.Fill(ds);

                comboBox2.DataSource = ds.Tables[0];
                comboBox2.DisplayMember = "State";
                comboBox2.ValueMember = "StateId";
                comboBox2.SelectedIndex = 0;
            }
        }
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox2.SelectedIndex >= 0)
            {
                SqlDataAdapter da = new SqlDataAdapter("Select * From StateCity Where StateId='" + comboBox2.SelectedValue + "'", con);
                DataSet ds = new DataSet();
                da.Fill(ds);
                comboBox3.DataSource = ds.Tables[0];
                comboBox3.DisplayMember = "City";
                comboBox3.ValueMember = "CityId";
                comboBox3.SelectedIndex = 0;
            }
        }
    }
}
 
Share this answer
 
v2
Comments
Member 10918596 2-Jul-14 6:05am    
bt output shows error like "Conversion failed when converting the varchar value 'System.Data.DataRowView' to data type int"
CHill60 2-Jul-14 6:51am    
On which line does the error occur?
CHill60 2-Jul-14 6:50am    
I've formatted your code for you. Note that when you paste something into a solution there is a pop-up appears to the right, where you can select a format style for what you are pasting in - if you choose "code block" it will surround your text with "pre" tags - it makes it easier to read.
It's also a good idea to explain to the OP what you changed and why
Member 10918596 2-Jul-14 6:54am    
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(comboBox1.SelectedIndex.ToString());
if (comboBox1.SelectedIndex >= 0)
{
SqlDataAdapter da = new SqlDataAdapter("Select * From CountryState Where CountryId='" + comboBox1.SelectedValue + "'", con);
DataSet ds = new DataSet();
da.Fill(ds);

comboBox2.DataSource = ds.Tables[0];
comboBox2.DisplayMember = "State";

da.Fill(ds);-this line shows error....Conversion failed when converting the varchar value 'System.Data.DataRowView' to data type int

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900