Hi,
I have a vb.net webform where the grid page have search options.
One option is to select the items from a dropdown list: Items: M Number or Surname, this will bring up a search box for user to type in surname or m number and then a search button. This should bring up the results to grid.
SQL stored procedure
ALTER PROCEDURE [dbo].[usp_GetRecordbySurnamePMRef]
(
@LastName nvarchar(100) = NULL,
@PMRef nvarchar(50) = NULL
)
AS
BEGIN
SET NOCOUNT ON;
SELECT *
FROM Ab
WHERE (LastName = @LastName OR @LastName IS NULL)
AND (PMRef = @PMRef OR @PMRef IS NULL)
END
GO
the stored procedure does work if i put complete information such as Lastname or PMRef number (M Number) in full.
Eg: If the lastname is 'Smith' and i put Smith it brings the record back, but doesnt bring anything if i put 'Smi'.
Protected Sub btnsearch_Click(sender As Object, e As EventArgs) Handles btnsearch.Click
Dim sql As String = "usp_GetRecordbySurnamePMRef"
Try
Using myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("Database_Connection_Lookups").ConnectionString)
Dim myCommand As SqlCommand = New SqlCommand(sql, myConnection)
Dim RecordStatus As String = ddlGridfilter.SelectedValue
Dim LastName As String = Me.txtsearch.Text
Dim PMRef As String = Me.txtsearch.Text
myCommand.CommandType = CommandType.StoredProcedure
If Not String.IsNullOrEmpty(txtsearch.Text) Then
myCommand.Parameters.AddWithValue("@LastName", txtsearch.Text)
Else
myCommand.Parameters.AddWithValue("@LastName", DBNull.Value)
End If
If Not String.IsNullOrEmpty(txtsearch.Text) Then
myCommand.Parameters.AddWithValue("@PMRef", txtsearch.Text)
Else
myCommand.Parameters.AddWithValue("@PMRef", DBNull.Value)
End If
myConnection.Open()
Using sda As New SqlDataAdapter
myCommand.Connection = myConnection
sda.SelectCommand = myCommand
Using ds As New DataSet
sda.Fill(ds)
Me.GridView1.DataSource = ds
Me.GridView1.DataBind()
End Using
End Using
End Using
Catch ex As Exception
Throw
'Response.Redirect("error.aspx")
End Try
End Sub
Please advice.
Many Thanks