Here I have created sample that will help you out.Here I have filtered based on name and city.
HTML
<div>
Name :<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
City :<asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="Search" />
<hr />
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="ContactName" HeaderText="Name" ItemStyle-Width="150" />
<asp:BoundField DataField="City" HeaderText="City" ItemStyle-Width="150" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
</div>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BindCustomer();
}
}
protected void Search(object sender, EventArgs e)
{
BindCustomer();
}
private void BindCustomer()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("[Customers_Search]"))
{
if (!string.IsNullOrEmpty(txtName.Text))
{
cmd.Parameters.AddWithValue("@ContactName", txtName.Text.Trim());
}
if (!string.IsNullOrEmpty(txtCity.Text))
{
cmd.Parameters.AddWithValue("@City", txtCity.Text.Trim());
}
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
VB
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not Me.IsPostBack Then
BindCustomer()
End If
End Sub
Protected Sub Search(sender As Object, e As EventArgs)
BindCustomer()
End Sub
Private Sub BindCustomer()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("[Customers_Search]")
If Not String.IsNullOrEmpty(txtName.Text) Then
cmd.Parameters.AddWithValue("@ContactName", txtName.Text.Trim())
End If
If Not String.IsNullOrEmpty(txtCity.Text) Then
cmd.Parameters.AddWithValue("@City", txtCity.Text.Trim())
End If
Using sda As New SqlDataAdapter()
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Using
End Using
End Using
End Sub
Screenshot
