Can someone help me with wiring up my search parameters. I have txtboxes on my webpage and a search button. So far I can only search for an ID. How can I expand this to include searching for AssignedTo, Location, Department, Model, InkType, Status, AquiredDate?
Here is my code. Can any one help? Do I just keep adding parameters? How does the DataKeyNames work? I want to able to search by all the fields in my where statement and not always have to put in an ID...
Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' Define data objects
Dim conn As SqlConnection
Dim comm As SqlCommand
Dim reader As SqlDataReader
' Initialize connection
Dim connectionString As String = _
ConfigurationManager.ConnectionStrings( _
"PrinterTracking").ConnectionString
conn = New SqlConnection(connectionString)
' Create command
comm = New SqlCommand( _
"SELECT ID, AssignedTo, Location, Department, Model, InkType, Status, AquiredDate " & _
"FROM PrinterInfo WHERE ID LIKE @txtID", conn)
' Verify the ID is numeric
Dim ID As Integer
If (Not Integer.TryParse(txtID.Text, ID)) Then
' If the user didn't enter numeric ID...
'userLabel.Text = "Please enter a numeric ID!"
Else
' Add parameter
comm.Parameters.Add("@txtID", System.Data.SqlDbType.Int)
comm.Parameters("@txtID").Value = ID
' Enclose database code in Try-Catch-Finally
Try
' Open the connection
conn.Open()
' Execute the command
reader = comm.ExecuteReader()
' Display the requested data
If reader.HasRows() Then
' Fill the grid with data
grid.DataSource = reader
grid.DataKeyNames = New String() {"ID"}
grid.DataBind()
userLabel.Text = ""
Else
userLabel.Text = _
"There is no user with this ID: " & ID
End If
' Close the reader and the connection
reader.Close()
Catch
' Display error message
userLabel.Text = "Error retrieving user data."
Finally
' Close the connection
conn.Close()
End Try
End If
End Sub