Hi vijay9471,
Please refer below sample.
Refer below article for split string finction for passing comma separated string to where clause.
HTML
City 1 :
<asp:TextBox ID="txtCities" runat="server"></asp:TextBox>
<asp:Button Text="Search" runat="server" OnClick="Search" />
<br />
<br />
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="ContactName" HeaderText="ContactName" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
VB.Net
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Constring"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT ContactName, City, Country FROM Customers", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
this.gvCustomers.DataSource = dt;
this.gvCustomers.DataBind();
}
}
}
}
protected void Search(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Constring"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT ContactName, City, Country FROM Customers WHERE City IN (SELECT Item FROM dbo.SplitString(@City, ','))", con))
{
cmd.Parameters.AddWithValue("@City", txtCities.Text.Trim());
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
this.gvCustomers.DataSource = dt;
this.gvCustomers.DataBind();
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Private Sub BindGrid()
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("Constring").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT ContactName, City, Country FROM Customers", con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
Me.gvCustomers.DataSource = dt
Me.gvCustomers.DataBind()
End Using
End Using
End Using
End Sub
Protected Sub Search(ByVal sender As Object, ByVal e As EventArgs)
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("Constring").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT ContactName, City, Country FROM Customers WHERE City IN (SELECT Item FROM dbo.SplitString(@City, ','))", con)
cmd.Parameters.AddWithValue("@City", txtCities.Text.Trim())
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
Me.gvCustomers.DataSource = dt
Me.gvCustomers.DataBind()
End Using
End Using
End Using
End Sub
Screenshot