Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
        DialogResult dh = MessageBox.Show("DO YOU WANT TO INSERT ?", "CONFIRM YOUR INSERT", System.Windows.Forms.MessageBoxButtons.YesNo);
        if (dh != DialogResult.No)
        {
            SqlCommand cmd = new SqlCommand("exec insertcatego @description,@quality,@quantity,@priceUnit,@vat,@totalprice", con);
            cmd.Parameters.AddWithValue("@description", txtDesc.Text);
            cmd.Parameters.AddWithValue("@quality", comboBoxQuality.SelectedValue.ToString());
            cmd.Parameters.AddWithValue("@quantity", txtQant.Text);
            cmd.Parameters.AddWithValue("@priceUnit", txtpUnt.Text);
            cmd.Parameters.AddWithValue("@vat", txtVat.Text);
            cmd.Parameters.AddWithValue("@totalprice", txtTotP.Text);
            con.Close();
            con.Open();
            cmd.ExecuteNonQuery();
            frmcategory_Load(sender, e);
            MessageBox.Show("SUCCESSFULL INSERT");
        }
    }
    catch (Exception a)
    {
        MessageBox.Show(a.Message.ToString());
    }
}
Posted
Updated 1-Aug-12 5:19am
v5
Comments
Santhosh Kumar Jayaraman 1-Aug-12 8:48am    
At which line You are gettting error? Check the object which is null in the line
Malli_S 1-Aug-12 9:00am    
Where exactly you're getting the error? Find out the line where the error is thrown, and check for that object. Check whether the objects are initialized/created.
[no name] 1-Aug-12 9:08am    
You need to debug your own code. This is just a code dump.
Vani Kulkarni 1-Aug-12 9:14am    
Where are you getting the error? Mark the line.
Gilbertinino 1-Aug-12 10:42am    
try
{
DialogResult dh = MessageBox.Show("DO YOU WANT TO INSERT ?", "CONFIRM YOUR INSERT", System.Windows.Forms.MessageBoxButtons.YesNo);
if (dh != DialogResult.No)
{
SqlCommand cmd = new SqlCommand("exec insertcatego @description,@quality,@quantity,@priceUnit,@vat,@totalprice", con);
error is coming when i confirm my insert.

Corrected your code. you were missing to assign the SqlConnection object to SqlCommand command object.

C#
DialogResult dh = MessageBox.Show("DO YOU WANT TO INSERT ?", "CONFIRM YOUR INSERT", System.Windows.Forms.MessageBoxButtons.YesNo);
                if (dh != DialogResult.No)
                {
                    SqlCommand cmd = new SqlCommand("exec insertcatego @description,@quality,@quantity,@priceUnit,@vat,@totalprice", con);
                    cmd.Parameters.AddWithValue("@description", txtDesc.Text);
                    cmd.Parameters.AddWithValue("@quality", comboBoxQuality.SelectedValue.ToString());
                    cmd.Parameters.AddWithValue("@quantity", txtQant.Text);
                    cmd.Parameters.AddWithValue("@priceUnit", txtpUnt.Text);
                    cmd.Parameters.AddWithValue("@vat", txtVat.Text);
                    cmd.Parameters.AddWithValue("@totalprice", txtTotP.Text);
                    //con.Close(); 
                    cmd.Connection=new SqlConnection("your connectionString here" );//Added line of code
                    con.Open();
                    cmd.ExecuteNonQuery();
                    frmcategory_Load(sender, e);
                    MessageBox.Show("SUCCESSFULL INSERT");
                }
            }
            catch (Exception a)
            {
                MessageBox.Show(a.Message.ToString());
            }
        }


Good luck !!!
 
Share this answer
 
v2
Comments
Matt T Heffron 1-Aug-12 17:01pm    
It looks like he was setting the SeqConnection in the SqlCommand constructor, 2nd argument. As I commented above, con being null seems quite likely.
Refer the following link for the exception details:
Object reference not set to an instance of an object.[^]
 
Share this answer
 
   DialogResult dh = MessageBox.Show("DO YOU WANT TO INSERT ?", "CONFIRM YOUR INSERT", System.Windows.Forms.MessageBoxButtons.YesNo);
        if (dh != DialogResult.No)
        {
    con.Open();
    SqlCommand cmd = new SqlCommand("INSERT INTO  category(description,quality, priceUnit,vat,totalprice) VALUES( @description, @quality, @priceUnit, @vat, @totalprice)", conn);

            cmd.Parameters.AddWithValue("@description", txtDesc.Text);
            cmd.Parameters.AddWithValue("@quality", comboBoxQuality.SelectedValue.ToString());
            cmd.Parameters.AddWithValue("@quantity", txtQant.Text);
            cmd.Parameters.AddWithValue("@priceUnit", txtpUnt.Text);
            cmd.Parameters.AddWithValue("@vat", txtVat.Text);
            cmd.Parameters.AddWithValue("@totalprice", txtTotP.Text);

            cmd.ExecuteNonQuery();
            con.Close();
    frmcategory_Load(sender, e);

            MessageBox.Show("SUCCESSFULL INSERT");
        }
    }
    catch (Exception a)
    {
        MessageBox.Show(a.Message.ToString());
    }
}
C#

 
Share this answer
 

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