我想在c#.net中使用数据库返回值的自动完成文本框的代码

时间:2021-01-14 00:00:01

private void txtBoxSearch_TextChanged(object sender, EventArgs e) {

private void txtBoxSearch_TextChanged(object sender,EventArgs e){

        AutoCompleteStringCollection namecollection = new AutoCompleteStringCollection();

        SqlConnection con = new SqlConnection("connectionn string");
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        string searchFor = "%" + txtBoxSearch.Text + "%";
       com.CommandText = "select cust_nm from Customer_Info where  (cust_nm LIKE ' %  " + searchFor + " %') ";


        con.Open();
        cmd.Parameters.AddWithValue("@name", searchFor);
        SqlDataReader rea = cmd.ExecuteReader();
        if (rea.HasRows == true)
        {
            while (rea.Read())
                namecollection.Add(rea["name"].ToString());
        }
        rea.Close();

        txtBoxSearch.AutoCompleteMode = AutoCompleteMode.Suggest;
        txtBoxSearch.AutoCompleteSource = AutoCompleteSource.CustomSource;
        txtBoxSearch.AutoCompleteCustomSource = namecollection;

    }

i want textbox which is act as a search option

我想要充当搜索选项的文本框

1 个解决方案

#1


0  

A few problems here

这里有一些问题

  1. You set the CommandText on com but execute cmd

    您在com上设置CommandText但执行cmd

  2. You set % both on the variable and on the CommandText

    您在变量和CommandText上设置%

  3. You add a parameter but you don't have the parameter in the CommandText

    您添加了一个参数,但CommandText中没有该参数

#1


0  

A few problems here

这里有一些问题

  1. You set the CommandText on com but execute cmd

    您在com上设置CommandText但执行cmd

  2. You set % both on the variable and on the CommandText

    您在变量和CommandText上设置%

  3. You add a parameter but you don't have the parameter in the CommandText

    您添加了一个参数,但CommandText中没有该参数