Imports System.Data.SqlClient Public Class Form1 Inherits System.Windows.Forms.Form Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click Dim drEmployee As SqlDataReader Try ' Open connection to the database sqlConnection1.Open() ' Fill the parameter with the value retrieved from the text field sqlCommand1.Parameters("@Find").Value = txtFind.Text ' Execute the query drEmployee = sqlCommand1.ExecuteReader() ' Fill the list box with the values retrieved lbFound.Items.Clear() While (drEmployee.Read()) lbFound.Items.Add(drEmployee("FirstName").ToString() & " " & drEmployee("LastName").ToString()) End While Catch ex As Exception ' Print error message MessageBox.Show(ex.Message) Finally ' Close data reader object and database connection If (Not drEmployee Is Nothing) Then drEmployee.Close() End If If (sqlConnection1.State = ConnectionState.Open) Then sqlConnection1.Close() End If End Try End Sub |