|
-
December 13th, 2009, 10:57 AM
#1
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
-
December 13th, 2009, 12:18 PM
#2
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
-
December 13th, 2009, 07:18 PM
#3
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.
Last edited by dovobet; December 13th, 2009 at 07:50 PM.
-
December 14th, 2009, 05:36 AM
#4
Re: Problem searching a Table
Gladd I could help 
greetz kristof
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|