C#从sql自动完成文本框

时间:2016-10-27 03:55:45

标签: c# sql winforms

大家好日子。

我的问题是如何根据在另一个文本框中输入的特定数据自动填充另一个文本框。为了进一步解释,我的第一个文本框是从一个名为“code”的sql表中自动完成的。在该列旁边是一个名为“描述”的列。基于从代码列填充的数据,如何根据在第一个文本框中选择的值自动填充第二个文本框?我真的希望有意义。

这是我更新textbox1的代码,这很好用:

 private void liguaneaRxToolStripMenuItem_Click(object sender, EventArgs e) 
    {
        this.liguanea_LaneTableAdapter1.Fill(this.pharmaciesDataSet1.Liguanea_Lane);
        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());
        }
    }

现在这是textbox2的代码,我想根据从文本框1中选择的值填写一个值。例如。在我要编写的查询中:“SELECT description FROM dbo.Liguanea_Lane其中code =”无论什么值。“其中”无论什么值“是从textbox1收集的输入然后返回(填充)textbox2中附加的描述

 private void displayValIntoTextbox(string val ) //this function will auto complete the other textbox 
    {

        if (val == null) //this will check if the value is null 
        {
            MessageBox.Show("please enter a the correct code");
        }
        else
        {
            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= '"+val +"'"; // this query
                SqlCommand cmd = new SqlCommand(query, con);

                SqlDataReader dr = cmd.ExecuteReader();
                AutoCompleteStringCollection mycollection = new AutoCompleteStringCollection();
                if (dr.HasRows) // check if any info exist in database base off the query
                {
                    while (dr.Read())
                    {

                        mycollection.Add(dr.GetString(0));

                    }
                    textBox1.AutoCompleteCustomSource = mycollection;
                }
                else
                {
                    MessageBox.Show(val);
                }

                con.Close();
            }
            catch (SqlException sql)
            {
                MessageBox.Show(sql.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }

我希望大家都关注。哦,它也是一个Winform应用程序

1 个答案:

答案 0 :(得分:0)

试试这个 -

对于来自DB的自动完成文本框,创建一个id为'txtAutoComplete'

的文本框控件
<asp:TextBox ID="txtSearch" runat="server" />
<asp:HiddenField ID="hfCustomerId" runat="server" />

然后为ajax添加jquery插件

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.0.min.js" type="text/javascript"></script>

<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/jquery-ui.min.js" type="text/javascript"></script>

为自动填充添加此ajax脚本

 $(function () {
    $("[id$=txtSearch]").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: '<%=ResolveUrl("~/Default.aspx/GetCustomers") %>',
                data: "{ 'prefix': '" + request.term + "'}",
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    response($.map(data.d, function (item) {
                        return {
                            label: item.split('-')[0],
                            val: item.split('-')[1]
                        }
                    }))
                },
                error: function (response) {
                    alert(response.responseText);
                },
                failure: function (response) {
                    alert(response.responseText);
                }
            });
        },
        select: function (e, i) {
            $("[id$=hfCustomerId]").val(i.item.val);
        },
        minLength: 1
    });
});  

在Default.aspx / GetCustomers方法中,编写一个C#代码以获取数据库中的名称列表。

为css添加此样式表参考

<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/themes/blitzer/jquery-ui.css"
rel="Stylesheet" type="text/css" />