Click here to Skip to main content
15,889,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everyone,
I'm a beginner. I need help with my code. In my button, I get this error message after adding a new record in datagrid view:
object reference not set to an instance of an object

Here is my code:
C#
con = new SqlConnection(dbconnection.constring);
da.InsertCommand = new SqlCommand("INSERT INTO item VALUES (@item_name,@price)", con);
da.InsertCommand.Parameters.Add("@item_name", SqlDbType.NVarChar).Value = textBox1.Text;
da.InsertCommand.Parameters.Add("@price", SqlDbType.Money).Value = textBox2.Text;
con.Open();
da.InsertCommand.ExecuteNonQuery();
con.Close();

[Edit - Fixed grammar and added code block]
Posted
Updated 5-Jan-12 11:50am
v3
Comments
Prasad_Kulkarni 5-Jan-12 9:28am    
check your connection string.
Bachelorhood 5-Jan-12 20:15pm    
Check the connection string is initialized
devbtl 6-Jan-12 2:32am    
use simply sqlcommand and pass textbox values direct into query.

Hello.
can you post a little bit more code?
probably "da" or "dbconnection.constring" are not initialized.

C#
SqlDataAdapter da = new SqlDataAdapter();
SqlConnection con = new SqlConnection(dbconnection.constring);
da.InsertCommand = new SqlCommand("INSERT INTO item VALUES (@item_name,@price)", con);
da.InsertCommand.Parameters.Add("@item_name", SqlDbType.NVarChar).Value = textBox1.Text;
da.InsertCommand.Parameters.Add("@price", SqlDbType.Money).Value = textBox2.Text;
con.Open();
da.InsertCommand.ExecuteNonQuery();
con.Close();


try to check this link: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.insertcommand.aspx[^]

here you can find more useful information

Regards
Robert
 
Share this answer
 
v2
This is a bit off-topic, but why are you using the data adapter at all. As far as the code is shown, you create a command, set the parameters and execute the command. Adapter isn't necessary for this. So if you simply need to insert a row from few text boxes and you don't use any other adapter functionality in your code, you could possibly simplify this to:
C#
con = new SqlConnection(dbconnection.constring);
SqlCommand cmd = new SqlCommand("INSERT INTO item VALUES (@item_name,@price)", con);
cmd.Parameters.Add("@item_name", SqlDbType.NVarChar).Value = textBox1.Text;
cmd.Parameters.Add("@price", SqlDbType.Money).Value = textBox2.Text;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
 
Share this answer
 
Comments
Andreas Gieriet 5-Jan-12 15:41pm    
Or maybe wrapped into usings:

using(var con=new SqlConnection(dbconnection.constring))
using(var cmd=new SqlCommand("INSERT INTO item VALUES (@item_name,@price)",con))
{
...
}
Wendelius 5-Jan-12 15:43pm    
Yes, definitely and adding proper try..catch etc :)
Espen Harlinn 5-Jan-12 17:53pm    
Good advice :)
Wendelius 5-Jan-12 17:57pm    
Thanks :)
Below code worked for me .

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;   
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data.Sql;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            SqlConnection con = new SqlConnection("user id=dbuser;" +
                                       "password=pass@123;server=10.1.2.21;" +
                                       "Trusted_Connection=yes;" +
                                       "database=TestDB; " +
                                       "connection timeout=30");          
            SqlCommand cmd = new SqlCommand("INSERT INTO item VALUES (@item_name,@price)", con);
            cmd.Parameters.Add("@item_name", SqlDbType.NVarChar).Value = '1';
            cmd.Parameters.Add("@price", SqlDbType.Money).Value = 100;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

        }
    }
}
 
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