Rom
Rom

Reputation: 63

Select * From table Where column Like statement c#

I failed to get the correct result with this code in Form2:

conn.Open();
OleDbCommand cmd = new OleDbCommand("Select * From udbTable Where Username Like '" + f1.textBox1.Text + "%'", conn);
OleDbDataReader reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            label5.Text = reader["Username"].ToString();
        }
        conn.Close();

I have 3 samples data in the table, but i'm always getting the same result which is the first entry of the database. Whenever i input the last entry or second entry in the textbox1.Text, i still getting the first entry.

textbox1.Text is from Form1, and i set it's property Modification to Public. label5.text is the output.

Upvotes: 0

Views: 1157

Answers (1)

Feras Salim
Feras Salim

Reputation: 438

try this fix

conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection=conn;
command.CommandText = "Select * From udbTable Where Username Like ?";
cmd.Parameters.Add("@Username",OleDbType.VarChar);
cmd.Parameters["@Username"].Value=f1.textBox1.Text;
OleDbDataReader reader = cmd.ExecuteReader();

Upvotes: 1

Related Questions