Problem searching a Table
Hi
I am having a problem with the code below
Code:
private void surnameBox_Leave(object sender, EventArgs e)
{
string criteria = surnameBox.Text;
try
{
SqlCommand filterAccounts = new SqlCommand
("SELECT * FROM Accounts WHERE Surname = @criteria", connectionLine);
reader = filterAccounts.ExecuteReader();
while (reader.Read())
{
MessageBox.Show("1");
filterBox.Items.Add(reader.GetString(3).ToString() + ", " + reader.GetString(4).ToString());
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
connectionLine.Close();
}
Basically I want the matching records to be added to a listbox that exists on the same form as the textbox does. However, nothing appear in the textbox. The messagebox showing that the code has entered the while loop also does not appear... I am unsure whether it is my SQL statement that is wrong or not. Any help is much appreciate:)
Re: Problem searching a Table
try
SqlCommand filterAccounts = new SqlCommand
("SELECT * FROM Accounts WHERE Surname = '"+ criteria +"', connectionLine);
also.. is your connection open before you execute the code?
greetz
Kristof
Re: Problem searching a Table
Thank you that worked perfectly, you rock Kristoff:)
Another note for anyone with the same problem is to use varchar(MAX) when trying to use a "select from where" with a field in a Table.
Re: Problem searching a Table
Gladd I could help :)
greetz kristof