OleDB Select Statement

24,338

You dont have any error in this code, Your error can possibly be on your listbox_events.

Show your codes for the listbox part.

Share:
24,338
Harold
Author by

Harold

Updated on July 24, 2022

Comments

  • Harold
    Harold almost 2 years

    I have a search button in my program. What I want is every time I search I will type the lastname and when the lastname is equivalent to any lastname in the database, the firstname and the lastname of the person will be displayed in a listbox and whenever I click on the result in the listbox, the person's details will be displayed in the textboxes. The problem is when there are persons with same surname, their names are listed uniquely but when I click on the name, same details appear in the textboxes.

    Please help.

    Here's my code:

        connection.Open();
        OleDbCommand select = new OleDbCommand();
        select.Connection = connection;
        select.CommandText = "Select * From Accounts Where Lastname  = '" + searchtb.Text + "'";
        OleDbDataReader reader = select.ExecuteReader();
        while (reader.Read())
        {
            listBox1.Items.Add(reader[1].ToString() + "," + reader[2].ToString());
        }
        connection.Close();
    
    • WhozCraig
      WhozCraig about 11 years
      One thing right of the top that is wrong with this. it's wide-open to a SQL injection attack. Seriously consider parameterizing that input.
    • echavez
      echavez about 11 years
      Aside from being SQL-Injection prone. If you want to find people with just part of their lastnames you should use "Select * From Accounts Where Lastname Like '" + searchtb.Text + "%'"