SEARCH USING TEXTBOX VALUE AND WILDCARD CHARACTER

Tanuj Kumar

Total Post:134

Points:940
Posted by  Tanuj Kumar
C#
 1242  View(s)
Ratings:
Rate this:

I want to search the empid of a name that with the characters entered in my textbox. using wildcard characters. I wrote the statement

da = new SqlDataAdapter(

 "Select empID from emp where FirstName like ' "+textbox1.text+" ' % "

   , connstring);

da.Fill(ds);

Is this statement correct?

  1. Sumit Kesarwani

    Post:378

    Points:2694
    Re: Search using textbox value and wildcard character

    string sql = "SELECT empID " +

                  "FROM emp " +

                  "WHERE FirstName like @FirstName";

    using(var con = new SqlConnection(connstring))

    using (SqlCommand command = new SqlCommand(sql, con))

    {

        command.Parameters.AddWithValue("@FirstName",  textbox1.text + "%");

        using(var da = new SqlDataAdapter(command))

        {

            da.Fill(ds);

        }

    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!