C# 在组合框(C)上列出状态时出现问题

C# 在组合框(C)上列出状态时出现问题,c#,winforms,C#,Winforms,我很难在ComboBox2上列出目前唯一的巴西州 我有一个包含3个表的数据库,PaisesContrees、EstadosState和Cidadesccities,我试图使用国家编号获取州,并将其列在ComboBox2中,但它不起作用 我的代码 private void Account_Create_Load(object sender, EventArgs e) { SqlConnection con = new SqlConnection(@"Data Source=

我很难在ComboBox2上列出目前唯一的巴西州 我有一个包含3个表的数据库,PaisesContrees、EstadosState和Cidadesccities,我试图使用国家编号获取州,并将其列在ComboBox2中,但它不起作用

我的代码

private void Account_Create_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=WINDOWS-PC\SQLSERVER;Initial Catalog=World;Integrated Security=True");
        string sql = "Select * from Paises";
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataReader sdr;

        try
        {
            con.Open();
            sdr = cmd.ExecuteReader();

            while (sdr.Read())
            {
                string Pais = sdr.GetString(1);
                comboBox1.Items.Add(Pais);
            }
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=WINDOWS-PC\SQLSERVER;Initial Catalog=World;Integrated Security=True");
        string sql = "Select Cod from Paises where NomePT = '" + comboBox1.Text + "'";
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataReader sdr;

        try
        {
            con.Open();
            sdr = cmd.ExecuteReader();

            while (sdr.Read())
            {
                string Cod = sdr.GetValue(0).ToString();
                lbl1.Text = Cod;
            }
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {     
        SqlConnection con = new SqlConnection(@"Data Source=WINDOWS-PC\SQLSERVER;Initial Catalog=World;Integrated Security=True");
        string sql = "select Cod , Estados from Estados where PaisCod = " + lbl1.Text + "";
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataReader sdr;

        try
        {
            con.Open();
            sdr = cmd.ExecuteReader();

            while (sdr.Read())
            {          
                string Cod = sdr.GetValue(0).ToString();
                lbl2.Text = Cod;

                string Estado = sdr.GetString(1);
                comboBox2.Items.Add(Estado);     
            }
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=WINDOWS-PC\SQLSERVER;Initial Catalog=World;Integrated Security=True");
        string sql = "Select * from Cidades where Ci_Cod = " + lbl2.Text + "";
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataReader sdr;

        try
        {
            con.Open();
            sdr = cmd.ExecuteReader();

            while (sdr.Read())
            {
                string Cidade = sdr.GetString(1);
                comboBox3.Items.Add(Cidade);  
            }
        }

        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

我对C和Windows窗体有点陌生,我的代码里有什么东西

我已经想出了如何让它工作。 我无法确切解释我是如何制作的,但代码如下:

  private void Form1_Load(object sender, EventArgs e)
    {              
        SqlConnection con = new SqlConnection(@"Data Source=WINDOWS-PC\SQLSERVER;Initial Catalog=World;Integrated Security=True");
        string sql = "Select * from Paises";
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataReader sdr;

        try
        {
            con.Open();
            sdr = cmd.ExecuteReader();

            while (sdr.Read())
            {
                string Pais = sdr.GetString(4);
                comboBox1.Items.Add(Pais);
            }                                                                        
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=WINDOWS-PC\SQLSERVER;Initial Catalog=World;Integrated Security=True");
        string sql = "Select Cod from Paises where Nome = '" + comboBox1.SelectedItem + "'";
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataReader sdr;

        try
        {
            con.Open();
            sdr = cmd.ExecuteReader();

            while (sdr.Read())
            {
                string PaisCod = sdr.GetValue(0).ToString();
                Cod.Text = PaisCod;
            }


            sdr.Close();


            string a = "Select Estados from Estados where PaisCod = " + Cod.Text + "";
            SqlCommand cm1 = new SqlCommand(a , con);
            SqlDataReader sd1;

            sd1 = cm1.ExecuteReader();

            while(sd1.Read())
            {
                string aqw = sd1.GetString(0);
                comboBox2.Items.Add(aqw);
            }

            sd1.Close();     
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=WINDOWS-PC\SQLSERVER;Initial Catalog=World;Integrated Security=True");
        string b = "Select Cod from Estados where Estados = '" + comboBox2.Text + "'";
        SqlCommand cmd = new SqlCommand(b , con);
        SqlDataReader sdr;

        try
        {
            con.Open();
            sdr = cmd.ExecuteReader();

            while(sdr.Read())
            {
                string Cod = sdr.GetValue(0).ToString();
                Ci_Cod.Text = Cod;
            }

            sdr.Close();

            string c = "Select Cidade from Cidades where Ci_Cod = " + Ci_Cod.Text + "";
            SqlCommand cm1 = new SqlCommand(c , con);
            SqlDataReader sd1;

            sd1 = cm1.ExecuteReader();

            while(sd1.Read())
            {
                string Cidades = sd1.GetString(0);
                comboBox3.Items.Add(Cidades);
            }
        }

        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }   
是的,我的一些变量乱七八糟 因此,基本上,当表单加载时,Countries表将加载到CheckBox1上。
当用户选中国家复选框时,标签Cod接收国家代码,然后,复选框2自动选择PaisCod等于国家代码的州,当选择州时,标签Ci_Cod接收国家代码,最后,复选框3选择代码等于城市代码的城市。抱歉,如果这让人困惑,请在您不理解的情况下进行评论。

是否添加了项目?是的,我多次检查以确保它将正常运行。您是否尝试过调试它?我建议在SelectedIndexChanged方法中使用comboBox1.SelectedItem.ToString,而不是comboBox1.Text。那么它到底在哪里失败了呢?复选框2没有列出状态,它应该使用label1.Text上的数字来列出它,并从Estados向DB select Cod,Estados发送一个查询,其中paiscd=+lbl1.Text+。ComboBox3也应该有同样的功能。但是它不起作用-没有足够的信息来帮助你,告诉我们当它不起作用时你会出现什么症状?抛出异常,什么是异常消息?数据库中是否真的存在状态?等