Click here to Skip to main content
15,885,855 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When i put a text into a textbox using textchanged event the listview shows data repeatedly .

What I have tried:

i have already used this code below here ,
C#
string name = textboxsearch.Text;
if (textboxsearch.Text != "")
{
    con.Open();
    string str = "select * from all_record where title like'" + name + "%'";
    OleDbCommand cmd = new OleDbCommand(str, con);
    OleDbDataReader dr = cmd.ExecuteReader();
    if (dr.Read())
    {
        dr.Close();                    
        OleDbDataReader dr1 = cmd.ExecuteReader();
        while (dr1.Read())
        {
            listView1.Visible = true;
            ListViewItem item = new ListViewItem(dr1["title"].ToString());
            item.SubItems.Add(dr1["last_name"].ToString());
            item.SubItems.Add(dr1["Age"].ToString());
            listView1.Items.Add(item);
        }
        dr1.Close();
    }
    else
    {
        listView1.Visible = false;
        listView1.Items.Clear();
    }
    con.Close();
}
else
{
    listView1.Visible = false;
    listView1.Items.Clear();
}
Posted
Updated 26-Aug-16 3:48am
v2
Comments
Richard MacCutchan 26-Aug-16 8:39am    
Why do you create two OleDbDataReader objects?
Richard MacCutchan 26-Aug-16 8:46am    
Most likely because the TextChanged event gets fired for every character that is typed. You need some way of ensuring that you only call this code when the user has stopped typing.
Member 12704984 26-Aug-16 9:27am    
Richard u correct , have you any solution ?
Richard MacCutchan 26-Aug-16 9:42am    
You need to choose a different event (maybe LostFocus), or find some other way to determine when you have all the text. Much depends on the requirements of the application.
Member 12704984 26-Aug-16 9:51am    
viewed all events .

1 solution

try this
two datareaders is not necessary. and its good to clear the values initially on the event call, so that duplicates wont be there.

C#
string name = textboxsearch.Text;

      listView1.Visible = false;
      listView1.Items.Clear();
      if (name != "")
      {

          con.Open();
          string str = "select * from all_record where title like'@name%'";
          OleDbCommand cmd = new OleDbCommand(str, con);
          cmd.Parameters.Add("@name",name);
          OleDbDataReader dr1 = cmd.ExecuteReader();
              while (dr1.Read())
              {
                  listView1.Visible = true;
                  ListViewItem item = new ListViewItem(dr1["title"].ToString());
                  item.SubItems.Add(dr1["last_name"].ToString());
                  item.SubItems.Add(dr1["Age"].ToString());
                  listView1.Items.Add(item);
              }
              dr1.Close();
           con.Close();
       }

      }


refer this regarding sql injection[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900