ASPForums.Net RSS Feed additions to the content that appears on ASPForums.Net(c) 2019 All rights reserved.Send ComboBox selected value to another Form in Windows Application using C# and VB.Net<p>Hi <a class="username" rel="PSowmiya"> PSowmiya</a>,</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data; using System.Data.SqlClient;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data.SqlClient</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">private void button1_Click(object sender, EventArgs e) { Form2 f2 = new Form2(this.comboBox1.SelectedItem.ToString()); f2.Show(); }</pre> <p><strong>Form2.cs</strong></p> <pre class="brush: csharp">string comb; public Form2(string comb) { this.comb = comb; InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { string constr = @&#34;Server=.\SQL2005;DataBase=Test;UID=user;PWD=pass&#34;; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(&#34;SELECT * FROM Customers WHERE Country=@Country&#34;, con)) { cmd.Parameters.AddWithValue(&#34;@Country&#34;, comb.ToString()); using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { using (DataTable dt = new DataTable()) { da.Fill(dt); this.dataGridView1.DataSource = dt; } } } } }</pre> <p><strong>VB</strong></p> <p><strong>Form1.vb</strong></p> <pre class="brush: 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</pre> <p><strong>Form2.vb</strong></p> <pre class="brush: 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 = &#34;Server=.\SQL2005;DataBase=Test;UID=user;PWD=pass&#34; Using con As SqlConnection = New SqlConnection(constr) Using cmd As SqlCommand = New SqlCommand(&#34;SELECT * FROM Customers WHERE Country=@Country&#34;, con) cmd.Parameters.AddWithValue(&#34;@Country&#34;, 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</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><span style="text-decoration: underline;"><strong><img src="" alt="" width="290" height="189" /></strong></span></p>, 11 Nov 2018 23:24:30 GMT