Object reference not set to an instance of an object.

I get the following error while setting an insert command onto my data adaptor (second line):

Object reference not set to an instance of an object.

adptr_DataAdapter1 = new SqlDataAdapter(str_SQL1, cnn_DataConnection1);
adptr_DataAdapter1.InsertCommand.CommandText = "INSERT INTO tbl_test (str_field1) VALUES('val1')";

Any idea why this is happening




Answer this question

Object reference not set to an instance of an object.

  • ed_narayanan

    Create the SqlCommand object first:



      SqlDataAdapter da = new SqlDataAdapter();

      // Create the InsertCommand.   There are many ways to do this...
      da.InsertCommand = cnn_DataConnection1.CreateCommand();

      da.InsertCommand.InsertCommand = "INSERT INTO Customers (CustomerID) VALUES ('val1')";


     


  • Object reference not set to an instance of an object.