ASPForums.Net RSS Feed additions to the content that appears on ASPForums.Net(c) 2019 All rights reserved.Send value from one Windows Form to another using Property class in C# and VB.Net<p>Hi <a class="username" rel="PSowmiya"> PSowmiya</a>,</p> <p>Please refer below code is working-</p> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data.SqlClient; </pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data.SqlClient Imports VB.Form1</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <p><strong>Form1.cs</strong></p> <pre class="brush: csharp">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 = @&#34;Server=.\SQL2005;DataBase=Test;UID=;PWD=&#34;; conn.Open(); Sql = &#34;SELECT * FROM Customers;&#34;; cmd = new SqlCommand(Sql, conn); dr = cmd.ExecuteReader(); while (dr.Read()) { comboBox1.Items.Add(dr[&#34;CustomerId&#34;]); } } 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; } }</pre> <p><strong>Form2.cs</strong></p> <pre class="brush: csharp">string rollNO = Form1.comboboxvalue.StudNo; public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { string constr = @&#34;Server=.\SQL2005;DataBase=Test;UID=;PWD=&#34;; ; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(&#34;SELECT * FROM Customers WHERE CustomerId=@CustomerId&#34;, con)) { cmd.Parameters.AddWithValue(&#34;@CustomerId&#34;, rollNO); using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { DataTable dt = new DataTable(); da.Fill(dt); this.dataGridView1.DataSource = dt; } } } }</pre> <p><strong>VB.Net</strong></p> <p><strong>Form1.vb</strong></p> <pre class="brush: 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 = &#34;Server=.\SQL2005;DataBase=Test;UID=;PWD=&#34; conn.Open() Sql = &#34;SELECT * FROM Customers;&#34; cmd = New SqlCommand(Sql, conn) dr = cmd.ExecuteReader() While dr.Read() comboBox1.Items.Add(dr(&#34;CustomerId&#34;)) 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</pre> <p><strong>Form2.vb</strong></p> <pre class="brush: 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 = &#34;Server=.\SQL2005;DataBase=Test;UID=;PWD=&#34; Using con As SqlConnection = New SqlConnection(constr) Using cmd As SqlCommand = New SqlCommand(&#34;SELECT * FROM Customers WHERE CustomerId=@CustomerId&#34;, con) cmd.Parameters.AddWithValue(&#34;@CustomerId&#34;, 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</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="" alt="" width="305" height="139" /></p> <p>&nbsp;</p> <p>&nbsp;</p>, 13 Nov 2018 00:40:28 GMT