Hi nauna,
Check this sample. now take its reference.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:TextBox runat="server" ID="txtCountry" />
<asp:Button Text="Search" runat="server" OnClick="OnSearch" />
<hr />
<div style="width: 330px; height: 200px; overflow: auto;">
<asp:GridView runat="server" ID="gvCustomers" AutoGenerateColumns="false" Width="100%">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="Id" />
<asp:BoundField DataField="ContactName" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
</div>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
BindCustomers("");
}
protected void OnSearch(object sender, EventArgs e)
{
string country = txtCountry.Text.Trim();
this.BindCustomers(country);
}
private void BindCustomers(string country)
{
string query = "SELECT CustomerID,ContactName,Country FROM Customers ";
if (!string.IsNullOrEmpty(country))
{
string[] countryName = country.Split(' ');
List<string> countries = new List<string>();
foreach (string countryy in countryName)
{
countries.Add(countryy);
}
country = "'" + string.Join("','", countries.ToArray()) + "'";
query += " WHERE Country IN(" + country + ")";
}
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.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
BindCustomers("")
End Sub
Protected Sub OnSearch(ByVal sender As Object, ByVal e As EventArgs)
Dim country As String = txtCountry.Text.Trim()
Me.BindCustomers(country)
End Sub
Private Sub BindCustomers(ByVal country As String)
Dim query As String = "SELECT CustomerID,ContactName,Country FROM Customers "
If Not String.IsNullOrEmpty(country) Then
Dim countryName As String() = country.Split(" "c)
Dim countries As List(Of String) = New List(Of String)()
For Each countryy As String In countryName
countries.Add(countryy)
Next
country = "'" & String.Join("','", countries.ToArray()) & "'"
query += " WHERE Country IN(" & country & ")"
End If
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand(query, con)
cmd.CommandType = CommandType.Text
Using sda As SqlDataAdapter = New SqlDataAdapter()
sda.SelectCommand = cmd
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Me.gvCustomers.DataSource = dt
Me.gvCustomers.DataBind()
End Using
End Using
End Using
End Sub
Screenshot