c# - 根据一个文本框值从数据库自动填充文本框

标签 c# database winforms textbox

<分区>

我有一个简单的表单,其中有 3 个文本框,如下图所示: enter image description here

在 textchanged 事件上使用自动完成功能,我在 textbox1(人名)中显示来自数据库的数据。现在,如果您的用户从建议的项目中选择一个特定名称,我想根据 textbox1 的值自动填充数据库中的 textbox2 和 textbox3。 我应该怎么做?

文本框1的代码:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();
    SqlConnection con = new SqlConnection(@"***my connection string***");
    con.Open();
    SqlCommand cmnd = con.CreateCommand();
    cmnd.CommandType = CommandType.Text;
    cmnd.CommandText = "SELECT * FROM tblTicketDetail";
    SqlDataReader dReader;
    dReader = cmnd.ExecuteReader();

    if (dReader.Read())
    {
        while (dReader.Read())
            namesCollection.Add(dReader["ContactPerson"].ToString());
    }
    else
    {
        MessageBox.Show("Data not found");
    }
    dReader.Close();

    textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
    textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
    textBox1.AutoCompleteCustomSource = namesCollection;
}

现在如何自动填充 textbox2 和 textbox3?

问候。

最佳答案

    try
    {
        int i = Convert.ToInt32(CBCompanies.SelectedValue.ToString());
        DataTable td = new DataTable();
        da = new SqlDataAdapter("select * from tblCustomerCompany where CustomerID =" + CBCompanies.SelectedValue.ToString() + "  ORDER BY CustomerName", conn);
        conn.Open();
        da.Fill(td);
        conn.Close();
        textBox3.Text = td.Rows[0].ItemArray[5].ToString();
    }
    catch { }  

关于c# - 根据一个文本框值从数据库自动填充文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21852192/

相关文章:

c# - 在 DataGridViewComboBoxColumn SelectedIndexChanged 期间触发的事件

c# - 处理每种形式的异常或只是在主要

c# - 通过网络读取文件非常慢

时间:2019-05-17 标签:c#datagridviewautorefreshinlan

c# - 对于 i = 0,为什么 (i += i++) 等于 0?

c# - byte + byte = int...为什么?

database - 是否建议在生产环境中使用数据库作为容器?

c# - 本地数据库插入未存储

c# - 我如何设计 Storage 类,使其适应任何存储类型,同时每种存储类型只有一个实例?

c# - 使用XML存储数据,如何使用Entity Framework?