Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a piece of code which is on a Windows Form. I need to Search using 2 values entered in 2 different Textboxes from a MySQL database (i am searching using Name and User-id if they exist in the database and if not display proper error messages). Can someone please explain how this can be done with an example.

Thanks,
Tushar.
Posted
Comments
[no name] 10-Jul-12 14:12pm    
What have you tried?
tusharmathankar 10-Jul-12 14:15pm    
Just a simple select * from table where column_name like '"% Textbox2.text %"' I am not sure how to count the number of rows in the table and then check for the end of the Table and then return the appropriate values when matched

1 solution

Try:
SQL
SELECT * FROM myTable WHERE name='Name' AND userId='UserId'
Just do it with a parametrized query:

C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT iD, description FROM myTable WHERE userName=@UN AND userId=@UI", con))
        {
        cmd.Parameters.AddWithValue("@UN", myUserNameTextBox.Text);
        cmd.Parameters.AddWithValue("@UI", myUserIdTextBox.Text);
        using (SqlDataReader reader = cmd.ExecuteReader())
            {
            while (reader.Read())
                {
                int id = (int) reader["iD"];
                string desc = (string) reader["description"];
                Console.WriteLine("ID: {0}\n    {1}", iD, desc);
                }
            }
        }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900