开发者

Filtering the list C# ms access

开发者 https://www.devze.com 2023-04-12 12:12 出处:网络
My plan is when I insert the letter M, the whole Letter M at the beginning of a word will show using the listbox but I can\'t do it.I used this code but I can\'t find why it wasn\'t working:

My plan is when I insert the letter M, the whole Letter M at the beginning of a word will show using the listbox but I can't do it. I used this code but I can't find why it wasn't working:

conn.Open();
OleDbCommand cmd2 = new OleDbCommand("SELECT fnID, Lastname, Firstname, Middlename FROM tbl_Fullname WHER开发者_JS百科E Firstname LIKE '%?'", conn);
cmd2.Parameters.Add("@Firstname", OleDbType.VarChar).Value = textBox3.Text;
try
{
  OleDbDataReader dr = cmd2.ExecuteReader();
  if (dr.Read())
  {
    textBox1.Text = dr[0].ToString();   //fnID
    listBox1.Items.Add(dr[1].ToString()); //Lastname 
    textBox3.Text = dr[2].ToString();   //Firstname
    textBox4.Text = dr[3].ToString();   //Middlename
  }
  else
  {
    textBox1.Text = "";
    textBox2.Text = "";
    textBox3.Text = "";
    textBox4.Text = "";
    //MessageBox.Show("No result");
  }
}
catch (Exception ex)
{
  MessageBox.Show(ex.Message);
}
conn.Close();


Your query is wrong:

LIKE '%?'

Firstly % is the wildcard so it needs to come after the parameter

Secondly I'm not sure that that's going to work at all - in fact I'm fairly certain it won't, i.e. you can't put the parameter into a quoted string - so I think you want the following in the query

LIKE ?

And for the param setting to be something like:

.Value = textBox3.Text + "%"


this is my new code for filtering:

conn.Open();
            OleDbCommand cmd2 = new OleDbCommand("SELECT fnID,Lastname,Firstname,Middlename FROM tbl_Fullname WHERE Firstname LIKE '" + textBox3.Text + "%'", conn);

            try
            {
                OleDbDataReader dr = cmd2.ExecuteReader();

                while (dr.Read())
                {
                    textBox1.Text = dr[0].ToString();   //fnID
                    textBox2.Text = dr[1].ToString();
                    listBox1.Items.Add(dr[1].ToString()); //Lastname
                    textBox3.Text = dr[2].ToString();   //Firstname
                    textBox4.Text = dr[3].ToString();   //Middlename
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            conn.Close();

It worked well. i don't know if there is a bug with it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号