Hi PSowmiya,
Please refer below sample.
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Data.SqlClient
Code
C#
Form1.cs
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this.comboBox1.SelectedItem.ToString());
f2.Show();
}
Form2.cs
string comb;
public Form2(string comb)
{
this.comb = comb;
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
string constr = @"Server=.\SQL2005;DataBase=Test;UID=user;PWD=pass";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers WHERE Country=@Country", con))
{
cmd.Parameters.AddWithValue("@Country", comb.ToString());
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
da.Fill(dt);
this.dataGridView1.DataSource = dt;
}
}
}
}
}
VB
Form1.vb
Private Sub button1_Click(sender As System.Object, e As System.EventArgs) Handles button1.Click
Dim f2 As Form2 = New Form2(Me.comboBox1.SelectedItem.ToString())
f2.Show()
End Sub
Form2.vb
Private comb As String
Public Sub New(ByVal comb As String)
Me.comb = comb
InitializeComponent()
End Sub
Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim constr As String = "Server=.\SQL2005;DataBase=Test;UID=user;PWD=pass"
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Customers WHERE Country=@Country", con)
cmd.Parameters.AddWithValue("@Country", comb.ToString())
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
da.Fill(dt)
Me.DataGridView1.DataSource = dt
End Using
End Using
End Using
End Using
End Sub
Screenshot
