hi everyone. can someone tell me whether the sql command is right because i get a blank gridview when i click the search button. The button was supposed to search the keywords entered in a textbox from 2 tables using wildcards. thank you in advance
Private Sub bnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bnSearch.Click
Dim myConnection As New SqlConnection(myConStr)
Dim mySqlCommand As New SqlCommand()
Dim mySqlDataReader As SqlDataReader
Dim keywords As String
Dim sqlStr As String
keywords = "'%'" & txtSearch.Text
sqlStr = "SELECT tblBooks.fldTitle, tblBooks.fldAuthor, tblBooks.fldISBN, tblBooks.fldNoCopies, tblBooks.fldInStock, tblCat.fldCatName FROM tblBooks INNER JOIN tblCat ON tblBooks.fldCatID = tblCat.fldCatID WHERE (fldTitle LIKE @keywords) AND (fldAuthor LIKE @keywords) AND (fldCatName LIKE @keywords) AND (fldISBN LIKE @keywords)ORDER BY tblBooks.fldTitle"
mySqlCommand.Parameters.AddWithValue("@keywords", keywords)
mySqlCommand.Connection = myConnection
mySqlCommand.CommandText = sqlStr
myConnection.Open()
mySqlDataReader = mySqlCommand.ExecuteReader
gvBookList.DataSource = mySqlDataReader
gvBookList.DataBind()
End Sub
darksyfer
Member
17 Points
37 Posts
sql commnd query strings
Apr 29, 2012 01:35 PM|LINK
hi everyone. can someone tell me whether the sql command is right because i get a blank gridview when i click the search button. The button was supposed to search the keywords entered in a textbox from 2 tables using wildcards. thank you in advance
Private Sub bnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bnSearch.Click Dim myConnection As New SqlConnection(myConStr) Dim mySqlCommand As New SqlCommand() Dim mySqlDataReader As SqlDataReader Dim keywords As String Dim sqlStr As String keywords = "'%'" & txtSearch.Text sqlStr = "SELECT tblBooks.fldTitle, tblBooks.fldAuthor, tblBooks.fldISBN, tblBooks.fldNoCopies, tblBooks.fldInStock, tblCat.fldCatName FROM tblBooks INNER JOIN tblCat ON tblBooks.fldCatID = tblCat.fldCatID WHERE (fldTitle LIKE @keywords) AND (fldAuthor LIKE @keywords) AND (fldCatName LIKE @keywords) AND (fldISBN LIKE @keywords)ORDER BY tblBooks.fldTitle" mySqlCommand.Parameters.AddWithValue("@keywords", keywords) mySqlCommand.Connection = myConnection mySqlCommand.CommandText = sqlStr myConnection.Open() mySqlDataReader = mySqlCommand.ExecuteReader gvBookList.DataSource = mySqlDataReader gvBookList.DataBind() End Sub