Hi akhter,
Refer below sample.
SQL
CREATE PROCEDURE SearchNameCounry
@Name VARCHAR(50)= NULL,
@Country VARCHAR(50) = NULL
AS
BEGIN
SELECT TOP 5 CompanyName
,ContactName
,Country
FROM Customers
WHERE (ContactName = @Name OR @Name IS NULL) OR (Country = @Country OR @Country IS NULL)
END
HTML
Name :
<asp:TextBox runat="server" ID="txtName" AutoPostBack="true" OnTextChanged="SearchByName" />
<br />
<br />
Country :
<asp:DropDownList runat="server" ID="ddlCountry" AutoPostBack="true" OnSelectedIndexChanged="SearchByCountry">
</asp:DropDownList>
<br />
<br />
<asp:GridView runat="server" AutoGenerateColumns="false" ID="gvCustomers">
<Columns>
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT Country FROM Customers", con))
{
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
ddlCountry.DataSource = dt;
ddlCountry.DataTextField = "Country";
ddlCountry.DataValueField = "Country";
ddlCountry.DataBind();
ddlCountry.Items.Insert(0, new ListItem("Please select"));
}
}
DataTable dt1 = GetData(txtName.Text, ddlCountry.SelectedValue);
gvCustomers.DataSource = dt1;
gvCustomers.DataBind();
}
}
protected void SearchByName(object sender, EventArgs e)
{
DataTable dt = GetData(txtName.Text, ddlCountry.SelectedValue);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
protected void SearchByCountry(object sender, EventArgs e)
{
DataTable dt = GetData(txtName.Text, ddlCountry.SelectedValue);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
private DataTable GetData(string name, string country)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SearchNameCounry", con))
{
cmd.CommandType = CommandType.StoredProcedure;
if (!string.IsNullOrEmpty(name))
{
cmd.Parameters.AddWithValue("@Name", name);
}
else
{
cmd.Parameters.AddWithValue("@Name", "");
}
if (!string.IsNullOrEmpty(country))
{
cmd.Parameters.AddWithValue("@Country", country);
}
else
{
cmd.Parameters.AddWithValue("@Country", "");
}
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT Country FROM Customers", con)
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
ddlCountry.DataSource = dt
ddlCountry.DataTextField = "Country"
ddlCountry.DataValueField = "Country"
ddlCountry.DataBind()
ddlCountry.Items.Insert(0, New ListItem("Please select"))
End Using
End Using
Dim dt1 As DataTable = GetData(txtName.Text, ddlCountry.SelectedValue)
gvCustomers.DataSource = dt1
gvCustomers.DataBind()
End If
End Sub
Protected Sub SearchByName(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = GetData(txtName.Text, ddlCountry.SelectedValue)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Sub
Protected Sub SearchByCountry(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = GetData(txtName.Text, ddlCountry.SelectedValue)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Sub
Private Function GetData(ByVal name As String, ByVal country As String) As DataTable
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SearchNameCounry", con)
cmd.CommandType = CommandType.StoredProcedure
If Not String.IsNullOrEmpty(name) Then
cmd.Parameters.AddWithValue("@Name", name)
Else
cmd.Parameters.AddWithValue("@Name", "")
End If
If Not String.IsNullOrEmpty(country) Then
cmd.Parameters.AddWithValue("@Country", country)
Else
cmd.Parameters.AddWithValue("@Country", "")
End If
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
Return dt
End Using
End Using
End Function
Screenshot