如何根据从SQL中提取的第一个TextBox填充第二个TextBox?

时间:2016-10-28 17:00:24

标签: c# sql winforms

我的问题是根据第一个文本框自动填充数据库中的第二个文本框。

这是自动完成的TextBox,这个工作正常,这里没有问题。

private void textBox1_TextChanged(object sender, EventArgs e)
{
    try
    {
        string connectionString = "Data Source=LPMSW09000012JD\\SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True";
        SqlConnection con = new SqlConnection(connectionString);
        con.Open();
        string query = "SELECT Code FROM dbo.Liguanea_Lane";
        SqlCommand cmd = new SqlCommand(query, con);

        SqlDataReader dr = cmd.ExecuteReader();
        AutoCompleteStringCollection mycollection = new AutoCompleteStringCollection();
        while (dr.Read())
        {
            mycollection.Add(dr.GetString(0));
        }
        textBox1.AutoCompleteCustomSource = mycollection;
        con.Close();
    }
    catch (Exception ex)
    {    
        MessageBox.Show(ex.ToString());
    }    
}

这是第二个TextBox,它将根据第一个TextBox中的选择自动填充,并根据在第一个TextBox中选择的选项从SQL中过滤掉数据。我不确定我哪里出错了。请看一下:

private void textBox2_TextChanged(object sender, EventArgs e)
{               
    try
    {
        string connectionString = "Data Source=LPMSW09000012JD\\SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True";
        SqlConnection con = new SqlConnection(connectionString);
        con.Open();
        string query = "SELECT description FROM dbo.Liguanea_Lane where code= '" + textBox1.Text + "'"; // this query
        SqlCommand cmd = new SqlCommand(query, con);   
        con.Close();
    }
    catch (SqlException sql)
    {
        MessageBox.Show(sql.ToString());
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。我做的是切换到自动完成组合框。我首先创建了一个fillCombo方法,该方法使用SQL数据库中的项填充相应的comboBox

void fillCombo()
    {
           try
           {

               string connectionString = "Data Source=JAVY26;Initial Catalog=Pharmacies;Integrated Security=True";
               SqlConnection con = new SqlConnection(connectionString);
               con.Open();
               string query = "SELECT * FROM dbo.Liguanea_Lane";
               SqlCommand cmd = new SqlCommand(query, con);

               SqlDataReader dr = cmd.ExecuteReader();
               while (dr.Read())
               {
                   string scode = dr.GetString(dr.GetOrdinal("code"));
                   comboBox2.Items.Add(scode);
               }
           }
           catch (Exception ex)
           {

               MessageBox.Show(ex.ToString());
           }*/


    }

然后在comboBox中实现了代码,我创建了一个字符串变量来存储"描述" SQL数据库中的列,然后分配给文本框,该文本框将根据fillCombo方法的选择返回值。

   private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {

            string connectionString = "Data Source=JAVY26;Initial Catalog=Pharmacies;Integrated Security=True";
            string query = "SELECT * FROM dbo.Liguanea_Lane WHERE code = '" + comboBox2.Text + "' ; ";
            SqlConnection con = new SqlConnection(connectionString);
            con.Open();
            SqlCommand cmd = new SqlCommand(query, con);
            SqlDataReader dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                string sdes = dr.GetString(dr.GetOrdinal("description"));
                textBox5.Text = sdes;

            }

        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.ToString());
        }