CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2009
    Posts
    90

    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

  2. #2
    Join Date
    Sep 2006
    Posts
    31

    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

  3. #3
    Join Date
    Dec 2009
    Posts
    90

    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.

  4. #4
    Join Date
    Sep 2006
    Posts
    31

    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
  •  





Click Here to Expand Forum to Full Width

Featured