-1

I'm trying to save data from datagridview to database. I keep on getting a

NullReferenceException: Object reference not set to an instance of an object


try 
{           
    foreach (DataGridViewRow row in dataGridView2.Rows)
    {            
        using ( conn = new SqlConnection(constring))
        {
            using (SqlCommand cmd = new SqlCommand("INSERT INTO tbl_Students_Marks VALUES(@Student, @T1, @T2, @T3, @T4)", conn))
            {
                cmd.Parameters.AddWithValue("@Student", (row.Cells["Student Number"].Value).ToString());
                cmd.Parameters.AddWithValue("@T1", row.Cells["Test 1"].Value);
                cmd.Parameters.AddWithValue("@T2", row.Cells["Test 2"].Value);
                cmd.Parameters.AddWithValue("@T3", row.Cells["Test 3"].Value);
                cmd.Parameters.AddWithValue("@T4", row.Cells["Test 4"].Value);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
M_Magz
  • 43
  • 1
  • 7

3 Answers3

1

hmmm try this codes

try 
{           
foreach (DataGridViewRow row in dataGridView2.Rows)
{            
    using ( conn = new SqlConnection(constring))
    {
        using (SqlCommand cmd = new SqlCommand(constring))
        {
            cmd.Connection = con;
            cmd.CommandText = "INSERT INTO tbl_Students_Marks VALUES(@Student, @T1, @T2, @T3, @T4)";
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@Student", (row.Cells["Student Number"].Value).ToString());
            cmd.Parameters.AddWithValue("@T1", row.Cells["Test 1"].Value);
            cmd.Parameters.AddWithValue("@T2", row.Cells["Test 2"].Value);
            cmd.Parameters.AddWithValue("@T3", row.Cells["Test 3"].Value);
            cmd.Parameters.AddWithValue("@T4", row.Cells["Test 4"].Value);
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();

i hope it helps you

kulotskie
  • 331
  • 1
  • 16
1

Looks like your row value is empty.

Try to replace your line 9 with this code:

            var studentNumber = row.Cells["Student Number"].Value
            if (studentNumber == null || studentNumber is DBNull)
              continue;
            cmd.Parameters.AddWithValue("@Student", studentNumber.ToString());
Artem Kulikov
  • 2,250
  • 19
  • 32
  • @ArtemNow it giving me this error: ***The parameterized query '(@Student nvarchar(4000),@T1 nvarchar(4000),@T2 nvarchar(4000),@' expects the parameter '@T1', which was not supplied.*** – M_Magz Aug 03 '15 at 05:17
  • Line number with error again please (after your inserted my code). – Artem Kulikov Aug 03 '15 at 05:32
1

You should remove the empty row from the bottom of your DataGridView first. this is why you can not insert last row. like this (Set AllowUserToAddRows to False in the DataGridView Properties or Add dataGridView2.AllowUserToAddRows = false; in your filling DataGridView method):

dataGridView2.DataSource = ds.Tables[0];
dataGridView2.AllowUserToAddRows = false;

Then rest of your code to INSERT.

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109