sql question, LIKE search using variablehttp://forums.asp.net/t/210860.aspx/1?sql+question+LIKE+search+using+variableFri, 01 Aug 2003 19:01:51 -0400210860210860http://forums.asp.net/p/210860/210860.aspx/1?sql+question+LIKE+search+using+variablesql question, LIKE search using variable How can i do a LIKE search specified by the variable in text box??? For example, the user types a search word in the text box. The word is stored in variable @text. Now i want to perform a LIKE search using that variable. here's my sql statement and part of my code: myComm.CommandText = "SELECT firstName AS 'First Name' FROM Customer WHERE firstName LIKE '@text%' " myComm.Parameters.Add("@text", txtSearch.Text) myConn.Open() Dim myReader As SqlClient.SqlDataReader = myComm.ExecuteReader() DataGrid1.DataSource = myReader DataGrid1.DataBind() myConn.Close() I have a customer called 'David' in my database. When i searched for 'Dav' it doesn't return any rows. If i use myComm.CommandText = "SELECT firstName AS 'First Name' FROM Customer WHERE firstName = @text " , it works fine. Looks like the LIKE statement is wrong. Thanks for any help. 2003-05-05T05:36:27-04:00210896http://forums.asp.net/p/210860/210896.aspx/1?Re+sql+question+LIKE+search+using+variableRe: sql question, LIKE search using variable IIRC: myComm.CommandText = "SELECT firstName AS 'First Name' FROM Customer WHERE firstName LIKE @text + '%' " Cheers Ken 2003-05-05T06:50:21-04:00211647http://forums.asp.net/p/210860/211647.aspx/1?Re+sql+question+LIKE+search+using+variableRe: sql question, LIKE search using variable Whoa - sorry - my mistake.... Ignore me 2003-05-05T23:17:12-04:00297666http://forums.asp.net/p/210860/297666.aspx/1?Re+sql+question+LIKE+search+using+variableRe: sql question, LIKE search using variable The correction: myComm.CommandText = "SELECT firstName AS 'First Name' FROM Customer WHERE firstName LIKE '" & txtSearch.Text & "%'" '- you don't need this next line. '- you'd use @text only when calling a stored procedure, but in this case, you are ' using embedded SQL. 'myComm.Parameters.Add("@text", txtSearch.Text) myConn.Open() Dim myReader As SqlClient.SqlDataReader = myComm.ExecuteReader() DataGrid1.DataSource = myReader DataGrid1.DataBind() myConn.Close() 2003-08-01T18:50:52-04:00297683http://forums.asp.net/p/210860/297683.aspx/1?Re+sql+question+LIKE+search+using+variableRe: sql question, LIKE search using variable Bit old thread, huh? Using named parameter in embedded SQL is completely valid as was demonstrated. It is not limited to stored procedures. In fact, if you want to avoid SQL injection, it is pretty recommendable to use parameters and not to use string concatenation. 2003-08-01T19:01:50-04:00