Send ComboBox selected value to another Form in Windows Application using C# and VB.Net

Last Reply 4 months ago By pandeyism

Posted 4 months ago

I have Two forms.I have to get selecteditem of First form's Combobox from second form and pass it as parameter to a Mysql Query.

First form's code is:

    public partial class SelectStudents : Form
    {
       
        public String Sql;
        public MySqlCommand cmd = new MySqlCommand();
        public MySqlConnection conn = new MySqlConnection();
        public MySqlDataReader dr;
        public SelectStudents()
        {
            InitializeComponent();
        }
       
        private void button1_Click(object sender, EventArgs e)
        {
           
            Payment p = new Payment();
            p.Show();
           
        }
    }

    public static class comboboxvalue
    {
        public static List<string> getstudentNo()
        {
            SelectStudents s1 = new SelectStudents();
            List<String> studentno = new List<String>();
            studentno.Add(s1.studentsNoCB.SelectedItem.ToString());
            return studentno;
        }
    }

Second form's code is:

            SelectStudents s = new SelectStudents();
            List <string> studentno = comboboxvalue.getstudentNo();
           
            try
            {

                conn1.ConnectionString = "server=localhost;user id=root;password=;database=studentfeescollection;Convert Zero Datetime=True";
                conn1.Open();
                Sql = "SELECT COUNT(AdmissionNo) as COUNT FROM tblfeescollection WHERE AdmissionNo='" +studentno  + "' AND FeesMonth='" + FeesofMonthCB.SelectedItem + "';";
                cmd = new MySqlCommand(Sql, conn1);
                dr1 = cmd.ExecuteReader();
                while (dr1.Read())
                {                    
                        if (dr1.GetString("COUNT")!= null) {
                        string count1 = dr1.GetString("COUNT").Trim();
                        count = Convert.ToInt32(count1);

                    }
                }
                dr1.Close();
                cmd.Dispose();
                conn1.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
           

and the error is 

Object reference not set to an instance of an object

Posted 4 months ago

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