简体   繁体   English

C#从SQL自动完成到文本框

[英]C# autocomplete to textbox from sql

Good day everyone. 今天是个好日子。

My question is how to automatically populate another text box based on the particular data entered within another. 我的问题是如何根据在另一个文本框中输入的特定数据自动填充另一个文本框。 To explain further, my first text box is auto completing from a sql table with the name "code." 为了进一步说明,我的第一个文本框是从名称为“ code”的sql表自动完成的。 beside that column is one called "description. " Based on that data being populated from the code column,how do I auto populate the second text box based on the value selected in the first text box? 在该列旁边是一个称为“描述”的字段。基于从代码列填充的数据,我如何根据在第一个文本框中选择的值自动填充第二个文本框? I really hope am making sense. 我真的希望有道理。

This is the code I have which is updating textbox1, this is working fine: 这是我正在更新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());
        }
    }

Now this is the code for textbox2 which I want to fill in a value based on the value selected from text box 1.So for eg. 现在这是textbox2的代码,我想根据从文本框1中选择的值填写一个值。 In a query where I would write: "SELECT description FROM dbo.Liguanea_Lane where code= "whatever value." Where "whatever value" is what would be the input collected from the textbox1 and then return(populate) the description attached to in textbox2 在查询中,我将写为:“从dbo.Liguanea_Lane中选择描述,其中代码=“任何值。”其中“任何值”是从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());
            }
        }
    }

I hope everyone follows. 我希望每个人都跟随。 Oh it is also a Winform application 哦,它也是Winform应用程序

Try this- 尝试这个-

For Auto complete textbox from DB create a textbox control with id 'txtAutoComplete' 对于从数据库自动完成文本框,请创建一个ID为'txtAutoComplete'的文本框控件

<asp:TextBox ID="txtSearch" runat="server" />
<asp:HiddenField ID="hfCustomerId" runat="server" />

then add jquery plugin for ajax 然后为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>

Add this ajax script for autocomplete 添加此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
    });
});  

in Default.aspx/GetCustomers method , write a C# code to get list of names from database. 在Default.aspx / GetCustomers方法中,编写C#代码以从数据库中获取名称列表。

for css add this style sheet reference 对于CSS添加此样式表参考

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM