Private Sub btnSearch_Click(ByVal sender As System.Object, 
                   ByVal e As System.EventArgs) Handles btnSearch.Click
    Dim strConnectionString As String = 
                 "Data Source=(local);Initial Catalog=pubs;
                                    User ID=pubs;Password=search"
    Dim strQuery As String = "SELECT * FROM titles"
    If txtSearch.Text.Length > 0 Then
        strQuery &= " WHERE title LIKE '%" & txtSearch.Text & "%'"
    End If
    Dim objAdapter As New SqlClient.SqlDataAdapter
    objAdapter.SelectCommand = New SqlClient.SqlCommand(strQuery, 
                  New SqlClient.SqlConnection(strConnectionString))
    Dim objDataSet As New DataSet
    objAdapter.Fill(objDataSet)

    grdResults.DataSource = objDataSet.Tables(0)
    grdResults.DataBind()
End Sub

Example 3: Retrieving data and binding to the DataGrid.

Back to Article