簡體   English   中英

C#從SQL自動完成到文本框

[英]C# autocomplete to textbox from sql

今天是個好日子。

我的問題是如何根據在另一個文本框中輸入的特定數據自動填充另一個文本框。 為了進一步說明,我的第一個文本框是從名稱為“ 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中選擇的值填寫一個值。 在查詢中,我將寫為:“從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());
            }
        }
    }

我希望每個人都跟隨。 哦,它也是Winform應用程序

嘗試這個-

對於從數據庫自動完成文本框,請創建一個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" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM