Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
0.00/5 (No 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.DataSource = ds.Tables[0];
            comboBox1.SelectedIndex = -1;
            comboBox1.DisplayMember = "County";
            comboBox1.ValueMember = "Countryid";
            
            
        }

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

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex >=0)
            {
                MessageBox.Show(comboBox1.SelectedIndex.ToString());
                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 comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        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 3-Jul-14 2:13am
v3
Comments
Kornfeld Eliyahu Peter 2-Jul-14 4:17am    
All your code is irrelevant as it does not show the line with the error! however if you copy-pasted the error you have a clear case error in the property name...
syed shanu 2-Jul-14 4:28am    
Where do you get error give more details and be specific in your error part code.
Rahul VB 3-Jul-14 8:12am    
Can we have a briefing about what you are trying to achieve? Where is the exception? etc etc etc

1 solution

C#
private void Form1_Load(object sender, EventArgs e)
{
    comboBox1.SelectedIndexChanged -= comboBox1_SelectedIndexChanged;
    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.SelectedIndex = -1;
    comboBox1.DisplayMember = "County";
    comboBox1.ValueMember = "Countryid";
    comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
}
 
Share this answer
 

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