Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why am I getting the exception "Object reference not set to an instance" ?
My cs code is given below
C#
private void button1_Click(object sender, EventArgs e)
        {

            string connetionString = null;
            SqlConnection cnn;
            connetionString = "Data Source=JDPANDEY-PC;Initial Catalog=amsco;User ID = sa;Password= 123";
            cnn = new SqlConnection(connetionString);
            SqlTransaction tr = null;
            cnn.Open();
            tr = cnn.BeginTransaction();
            try
            {
               // MessageBox.Show("Connection Open ! ");
                SqlCommand cmd = new SqlCommand("insert into registration (eid,ename,fname,dob,age,sex,marital,address,contact,qualification,djoining,jsalary,csalary,designation,department) values (@v1,@v2,@v3,@v4,@v5,@v6,@v7,@v8,@v9,@v10,@v11,@v12,@v13,@v14,@v15)");
                //,@cont,@rtype,@tclass,@board,@city,@pcode,@state,@payment)");
                cmd.Transaction = tr;
                cmd.Parameters.AddWithValue("@v1", textBox1.Text);
                cmd.Parameters.AddWithValue("@v2", textBox2.Text);
                cmd.Parameters.AddWithValue("@v3", textBox3.Text);
                cmd.Parameters.AddWithValue("@v4", textBox4.Text);
                cmd.Parameters.AddWithValue("@v5", textBox5.Text);
                cmd.Parameters.AddWithValue("@v6", comboBox1.SelectedItem.ToString());
                cmd.Parameters.AddWithValue("@v7", comboBox2.SelectedItem.ToString());
                cmd.Parameters.AddWithValue("@v8", textBox6.Text);
                cmd.Parameters.AddWithValue("@v9", textBox7.Text);
                cmd.Parameters.AddWithValue("@v10", comboBox3.SelectedItem.ToString());
                cmd.Parameters.AddWithValue("@v11", textBox8.Text);
                cmd.Parameters.AddWithValue("@v12", textBox9.Text);
                cmd.Parameters.AddWithValue("@v13", textBox10.Text);
                cmd.Parameters.AddWithValue("@v14", textBox11.Text);
                cmd.Parameters.AddWithValue("@v15", textBox12.Text);
                cmd.Connection = cnn;
                cmd.ExecuteNonQuery();
                tr.Commit();

            }
            catch (Exception ex)
            {
                //MessageBox.Show("Can not open connection ! ");
                MessageBox.Show(ex.Message);
            }
            finally
            {
                cnn.Close();
            }
        }
Posted
Updated 20-Mar-12 21:50pm
v2
Comments
uspatel 21-Mar-12 3:52am    
use breakpoint and see which line cause error?
Wayne Gaylard 21-Mar-12 3:54am    
Have you stepped through the code and found out exactly where this exception is being thrown. A null reference exception generally means that you are trying to access something that has not been instantiated yet.

1 solution

I see some comboBoxN.SelectedItem.ToString() calls. These can result in a NullReferenceException: the SelectedItem property can be null, when nothing is selected in the combobox, and consequently the ToString() call results in that exception.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Mar-12 4:43am    
Good catch, a 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900