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.
Author by
Harold
Updated on July 24, 2022Comments
-
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 thelastname
is equivalent to anylastname
in the database, thefirstname
and thelastname
of the person will be displayed in alistbox
and whenever I click on the result in thelistbox
, the person's details will be displayed in thetextboxes
. 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 thetextboxes
.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 about 11 yearsOne 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 about 11 yearsAside 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 + "%'"
-