Hi rajeesh,
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
Namespaces
C#
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Collections.Generic
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
private void Form1_Load(object sender, EventArgs e)
{
BindCustomers(txtCountry.Text.Trim());
}
private void btnSearch_Click(object sender, EventArgs e)
{
string country = txtCountry.Text.Trim();
this.BindCustomers(country);
}
private void BindCustomers(string country)
{
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()) + "'";
}
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
string query = "SELECT CustomerID,ContactName,City,Country FROM Customers WHERE Country IN (" + country + ")";
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
DataTable dt = new System.Data.DataTable();
sda.Fill(dt);
this.dataGridView1.DataSource = dt;
}
}
}
}
VB.Net
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
BindCustomers(txtCountry.Text.Trim())
End Sub
Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click
Dim country As String = txtCountry.Text.Trim()
Me.BindCustomers(country)
End Sub
Private Sub BindCustomers(ByVal country As String)
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()) & "'"
End If
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Dim query As String = "SELECT CustomerID,ContactName,City,Country FROM Customers WHERE Country IN (" & country & ")"
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 System.Data.DataTable()
sda.Fill(dt)
Me.dataGridView1.DataSource = dt
End Using
End Using
End Using
End Sub
Screenshot