0

i have created that table on my sql server :

create table Empolyee(
ESSN int not null,
EFirstName varchar(20) not null ,
ELastName varchar(20) not null,
EJob varchar(20),
EBDate date,
ESalary int,

primary key(ESSN));

and in C# i try to add data to database using that code :

SqlConnection sqc = new SqlConnection("Data Source=.\\;Initial Catalog=DB;Integrated Security=True");
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();

        da.SelectCommand = new SqlCommand("INSERT INTO Empolyee VALUES (@ESSN,@EFirstName,@ELastName,@EJob,@EBDate,@ESalary)", sqc);
        da.SelectCommand.Parameters.Add("@ESSN", SqlDbType.Int).Value = textBox1.Text;
        da.SelectCommand.Parameters.Add("@EFirstName", SqlDbType.VarChar).Value = textBox2.Text;
        da.SelectCommand.Parameters.Add("@ELastName", SqlDbType.VarChar).Value = textBox3.Text;
        da.SelectCommand.Parameters.Add("@EJob", SqlDbType.VarChar).Value = textBox4.Text;
        da.SelectCommand.Parameters.Add("@EBDate", SqlDbType.Date).Value = maskedTextBox1.Text;
        da.SelectCommand.Parameters.Add("@ESalary", SqlDbType.Int).Value = textBox5.Text;

        // open connection
        sqc.Open();
        // excute the command
        da.InsertCommand.ExecuteNonQuery();
        // close connection
        sqc.Close();

i get that error when i run : Object reference not set to an instance of an object or System.NullReferenceException: Object reference not set to an instance of an object.

4
  • Which line of code throws the exception? Commented Dec 26, 2011 at 14:41
  • Please tell us exactly where you get that error. Commented Dec 26, 2011 at 14:41
  • When doing the Insert you don't need a DataAdapter object or DataSet try looking at using a dataReader Commented Dec 26, 2011 at 14:41
  • da.InsertCommand.ExecuteNonQuery(); Commented Dec 26, 2011 at 14:44

2 Answers 2

2

you haven't assigned anything to da.InsertCommand - instead you have put an INSERT command into da.SelectCommand... just change all references from da.SelectCommand to da.InsertCommand in the code you show in your question:

    da.InsertCommand = new SqlCommand("INSERT INTO Empolyee VALUES (@ESSN,@EFirstName,@ELastName,@EJob,@EBDate,@ESalary)", sqc);
    da.InsertCommand.Parameters.Add("@ESSN", SqlDbType.Int).Value = textBox1.Text;
    da.InsertCommand.Parameters.Add("@EFirstName", SqlDbType.VarChar).Value = textBox2.Text;
    da.InsertCommand.Parameters.Add("@ELastName", SqlDbType.VarChar).Value = textBox3.Text;
    da.InsertCommand.Parameters.Add("@EJob", SqlDbType.VarChar).Value = textBox4.Text;
    da.InsertCommand.Parameters.Add("@EBDate", SqlDbType.Date).Value = maskedTextBox1.Text;
    da.InsertCommand.Parameters.Add("@ESalary", SqlDbType.Int).Value = textBox5.Text;

BTW you don't need any SqlDataAdapter or DataSet to execute an INSERT - it can be done with SqlConnection and SqlCommand only.

Sign up to request clarification or add additional context in comments.

2 Comments

OH MY GOSH , i can`t belive how come i choose selectcommand , thanks a lot , it works now
@R.Vector you are welcome :-) please don't forget to upvote/mark as accepted any answer that is of help (see meta.stackexchange.com/questions/5234/…).
2

you are executing the following line:

  da.InsertCommand.ExecuteNonQuery()

but you are init SelectCommand

Try to replace the following code:

da.InsertCommand= new SqlCommand("INSERT INTO Empolyee VALUES (@ESSN,@EFirstName,@ELastName,@EJob,@EBDate,@ESalary)", sqc);
        da.InsertCommand.Parameters.Add("@ESSN", SqlDbType.Int).Value = textBox1.Text;
        da.InsertCommand.Parameters.Add("@EFirstName", SqlDbType.VarChar).Value = textBox2.Text;
        da.InsertCommand.Parameters.Add("@ELastName", SqlDbType.VarChar).Value = textBox3.Text;
        da.InsertCommand.Parameters.Add("@EJob", SqlDbType.VarChar).Value = textBox4.Text;
        da.InsertCommand.Parameters.Add("@EBDate", SqlDbType.Date).Value = maskedTextBox1.Text;
        da.InsertCommand.Parameters.Add("@ESalary", SqlDbType.Int).Value = textBox5.Text;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.