Hi PSowmiya,
Please refer below code is working-
Namespaces
C#
using System.Data.SqlClient;
VB.Net
Imports System.Data.SqlClient
Imports VB.Form1
Code
C#
Form1.cs
public String Sql;
public SqlCommand cmd = new SqlCommand();
public SqlConnection conn = new SqlConnection();
public SqlDataReader dr;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
}
private void SelectStudents_Load(object sender, EventArgs e)
{
retrieveStudentsName();
}
public void retrieveStudentsName()
{
try
{
conn.ConnectionString = @"Server=.\SQL2005;DataBase=Test;UID=;PWD=";
conn.Open();
Sql = "SELECT * FROM Customers;";
cmd = new SqlCommand(Sql, conn);
dr = cmd.ExecuteReader();
while (dr.Read())
{
comboBox1.Items.Add(dr["CustomerId"]);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
cmd.Dispose();
dr.Close();
conn.Dispose();
}
}
private void studentsNoCB_SelectedIndexChanged(object sender, EventArgs e)
{
comboboxvalue.StudNo = this.comboBox1.Text.ToString();
}
public static class comboboxvalue
{
public static string StudNo;
public static string getstudentNo()
{
return StudNo;
}
}
Form2.cs
string rollNO = Form1.comboboxvalue.StudNo;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
string constr = @"Server=.\SQL2005;DataBase=Test;UID=;PWD="; ;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers WHERE CustomerId=@CustomerId", con))
{
cmd.Parameters.AddWithValue("@CustomerId", rollNO);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
this.dataGridView1.DataSource = dt;
}
}
}
}
VB.Net
Form1.vb
Public Sql As String
Public cmd As SqlCommand = New SqlCommand()
Public conn As SqlConnection = New SqlConnection()
Public dr As SqlDataReader
Public Sub New()
InitializeComponent()
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
Dim f2 As Form2 = New Form2()
f2.Show()
End Sub
Private Sub SelectStudents_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
retrieveStudentsName()
End Sub
Public Sub retrieveStudentsName()
Try
conn.ConnectionString = "Server=.\SQL2005;DataBase=Test;UID=;PWD="
conn.Open()
Sql = "SELECT * FROM Customers;"
cmd = New SqlCommand(Sql, conn)
dr = cmd.ExecuteReader()
While dr.Read()
comboBox1.Items.Add(dr("CustomerId"))
End While
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
cmd.Dispose()
dr.Close()
conn.Dispose()
End Try
End Sub
Private Sub studentsNoCB_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles comboBox1.SelectedIndexChanged
comboboxvalue.StudNo = Me.comboBox1.Text.ToString()
End Sub
Class comboboxvalue
Public Shared StudNo As String
Function getstudentNo() As String
Return StudNo
End Function
End Class
Form2.vb
Private rollNO As String = comboboxvalue.StudNo
Public Sub New()
InitializeComponent()
End Sub
Private Sub Form2_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim constr As String = "Server=.\SQL2005;DataBase=Test;UID=;PWD="
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Customers WHERE CustomerId=@CustomerId", con)
cmd.Parameters.AddWithValue("@CustomerId", rollNO)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
Me.dataGridView1.DataSource = dt
End Using
End Using
End Using
End Sub
Screenshot
