-2

I'm trying to update my database using datagridview but it throws "Object reference not set to an instance of an object" exception below is my code:

foreach (DataGridViewRow dgRow in dataGridView1.Rows)
{
   //string re = dgRow.Cells[0].Value.ToString();
   string re =Convert.ToString(dgRow.Cells[0].Value.ToString());
   string strQuery = "UPDATE unit_master SET unit='"+re.ToString()+"'";
   SqlCommand scmd = new SqlCommand(strQuery,SqlConn);
   scmd.ExecuteNonQuery();
}
SqlConn.Close();
Brian
  • 5,069
  • 7
  • 37
  • 47
  • 4
    Hello. Please use parametrized queries and tell us where the error is. Also, why are you calling `Convert.ToString()` on a string, and `.ToString()` on the string you got from converting the string to a string? – Ry- Dec 21 '13 at 03:51
  • Please see [this post](http://stackoverflow.com/questions/1943465/avoiding-null-reference-exceptions). – Brian Dec 21 '13 at 03:53
  • actually with above code data is getting updated but only exception getting throw anyway.and i did remove one of the .ToString conversion but then data is not getting updated. – user3115080 Dec 21 '13 at 03:56
  • You may want to have a "WHERE" clause else you're setting every value in the table to the same. – SQLMason Dec 21 '13 at 03:57
  • 2
    `Convert.ToString("Use a Debugger and step through it to find out why".ToString());` – Aniket Inge Dec 21 '13 at 03:58
  • 2
    Use a parametrized SQL instead. Your code is vulnerable to SQL injection. (see here the post from Jeff A. http://www.codinghorror.com/blog/2005/04/give-me-parameterized-sql-or-give-me-death.html) – turtlepick Dec 21 '13 at 04:04
  • thanks to all of u some part of it working some of it undone but i will figure it out,if i need some more help i will add a comment,thanks for your help it really helped me – user3115080 Dec 21 '13 at 04:16
  • Dan Andrews i did use WHERE clause but then data is not getting update,which was getting update before i use WHERE clause,i know there should have WHERE clause,but still i cant figure out what is wrong... – user3115080 Dec 21 '13 at 04:27
  • http://stackoverflow.com/users/276673/flaviotsf how should i use parametrized SQL since i dont know that much about SQL deeply – user3115080 Dec 21 '13 at 04:29

2 Answers2

2

You need to run your code in debug mode (F5 in Visual Studio, or click Debug->Start Debug) and trace it. The debugger should actually stop and show you what is null so you can then figure out why it is null. You can also set a breakpoint at the beginning of the loop and step through each line of the code so you can see what is happening. You can set watches on each variable so you can see the values at every step.

Most likely dgRow is null, or it's not converting to string correctly, or the Value of the column is null.

As a side note you're converting dgRow.Cells[0].Value to string twice... Value.ToString is already doing the conversion, so there is no need to call Convert.ToString() again.

CodeRedick
  • 7,346
  • 7
  • 46
  • 72
  • actually i did run code using debugger and showing Object reference not set to an instance of an object exception.yes i m doing twice time conversion because by only that way my data is getting updated – user3115080 Dec 21 '13 at 04:01
  • So you ran in debug, which is good. When it stopped at the exception did you check the value of each variable to see what was null? – CodeRedick Dec 21 '13 at 04:01
1

Problem 1: Please check whether you have initialised your SqlConnection instance variable properly.

Problem 2: The GridView cell may have null . If you call any function on null, it throws a NullReferenceException.

Suggestion: Use parameterised queries to avoid SQL injection attacks.

Try this:

SqlConn = new SqlConnection("/*your connection string*/");
foreach (DataGridViewRow dgRow in dataGridView1.Rows)
{
    if(dgRow.Cells[0].Value!=null)
    {
       string re =dgRow.Cells[0].Value.ToString();
       string strQuery = "UPDATE unit_master SET unit=@unit";
       SqlCommand scmd = new SqlCommand(strQuery,SqlConn);
       scmd.Parameters.AddWithValue("@unit",unit);
       scmd.ExecuteNonQuery();
    }
}
SqlConn.Close();
Ry-
  • 218,210
  • 55
  • 464
  • 476
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67