none
SqlCommand and Parameter.AddWithValue

    Question

  • I have an employee table.

     

    SqlConnection con = new SqlConnection(constr);

     

    SqlCommand cmd = new SqlCommand("select * from employee where FirstName like '%@EName%' or LastName like '%@EName%'", con);

    cmd.Parameters.AddWithValue(

    "@EName",EName);

    con.Open();

     

    SqlDataReader dr = cmd.ExecuteReader();

     

     

    This code is not woking.. reader contains no rows when executed. why?

    Friday, May 21, 2010 10:11 AM

Answers

  • Hi,

    The query in your code should be

    SqlCommand cmd = new SqlCommand("select * from employee where FirstName like '%' + @EName + '%' or LastName like '%' + @EName + '%'", con);

    • Marked as answer by karthiboom Friday, May 21, 2010 10:53 AM
    Friday, May 21, 2010 10:22 AM

All replies