Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am inserting record in sql while inserting into database following error is coming
"
Object reference not set to an instance of an object.
"
below is my c# code
C#
if (radioButton1.Checked)
            {
                sqlconnection sqlconn;
                SqlCommand sqlcmd = sqlconn.CreateCommand();
                //dd.putquertdata("insert into GI_sub0 values('001','" + textBox1.Text + "','" + textBox2.Text + "','admin','2011-03-04 00:00:00.000','10')");
                sqlcmd.CommandText = ("insert into GI_sub0 values('001','" + textBox1.Text + "','" + textBox2.Text + "','admin','2011-03-04 00:00:00.000','10')");
                try
                {
                    sqlcmd.ExecuteNonQuery();
                }
                catch (SqlException err)
                {
                    MessageBox.Show(err.Message);
                }
                MessageBox.Show("Record Entered Successfull");
            }
else messagebox.show("please select radioobtn");
Posted
Comments
Ashi0891 21-Aug-14 13:59pm    
have you provided the connection string? I don't think so!
and later add "sqlconn.open();" before "sqlcmd.ExecuteNonQuery();" or before try{}, and also add
finally
{
sqlconn.close();
}
after catch{}

or simply do as Solution 2.

You are not opening the Sql Connection.

Add sqlconn.open() before sqlcmd.ExecuteNonQuery()
 
Share this answer
 
Ignoring the typos in your question, you've declared a SqlConnection variable, but you haven't actually set it to anything before you try to call a method on it.

Also, your code is susceptible to SQL Injection[^]. You should be using parameterized queries instead of string concatenation.

C#
if (radioButton1.Checked)
{
    using (SqlConnection sqlconn = new SqlConnection("YOUR CONNECTION STRING HERE"))
    using (SqlCommand sqlcmd = sqlconn.CreateCommand())
    {
        // TODO: Give the parameters meaningful names.
        sqlcmd.CommandText = "insert into GI_sub0 values (@p0, @p1, @p2, @p3, @p4, @p5)";

        sqlcmd.Parameters.AddWithValue("@p0", "001");
        sqlcmd.Parameters.AddWithValue("@p1", textBox1.Text);
        sqlcmd.Parameters.AddWithValue("@p2", textBox2.Text);
        sqlcmd.Parameters.AddWithValue("@p3", "admin");
        sqlcmd.Parameters.AddWithValue("@p4", new DateTime(2011, 3, 4));
        sqlcmd.Parameters.AddWithValue("@p5", "10");

        try
        {
            sqlconn.Open();
            sqlcmd.ExecuteNonQuery();
            MessageBox.Show("Record Entered Successfull");
        }
        catch (SqlException err)
        {
            MessageBox.Show(err.Message);
        }
    }
}
 
Share this answer
 
Comments
Matt T Heffron 21-Aug-14 13:34pm    
+5
Member 10690757 21-Aug-14 14:24pm    
this solution is working ... but record can not be inserted in databse??? and table names are all correct?
then whats the matter?
Richard Deeming 21-Aug-14 14:25pm    
What's the error?
Member 10690757 21-Aug-14 14:30pm    
no error msg box is displayng that record entered but in sql record is not present... and if we make this query into sql then record will be entered???
Richard Deeming 21-Aug-14 14:32pm    
If you're not getting an error, then the query has executed, and the record has been added to the table. Make sure you're looking at the right server, database and table to find the new record.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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