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

J

Janet Heflin

Very new in the C# world so give me a break here. I have created two
web forms, one passes information to the other and brings up the data
based on the passed field. When I do the updateRow I get the object
recrence not set to an instance of an object. If I rem out the error
message on the update it goes through and acts as though it updates
but does not. Here is the updateRow statements:

private void updateRow(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
try
{
ErrorMessage.Text = "";
SqlCommand updCommand = new SqlCommand();
updCommand.Connection = sqlConnection1;
updCommand.CommandText = "UPDATE HSE Compliance SET Date = '"
+ ((TextBox)e.Item.Cells[2].Controls[0]).Text + "', Description =
'" + ((TextBox)e.Item.Cells[3].Controls[0]).Text + "', Manager = '" +
((TextBox)e.Item.Cells[4].Controls[0]).Text + "', Risk-ranking = '" +
((TextBox)e.Item.Cells[5].Controls[0]).Text + "', Status = '" +
((TextBox)e.Item.Cells[6].Controls[0]).Text + "'"
+ "WHERE Ref_No = '" + ((TextBox)e.Item.Cells[1].Controls[0]).Text
+ "'";
Label2.Text = "Updating Ref No " +
((TextBox)e.Item.Cells[1].Controls[0]).Text;
updCommand.CommandType = CommandType.Text;
sqlConnection1.Open();
updCommand.ExecuteNonQuery();
sqlDataAdapter1.Fill(dataSet1);
Cache["hse_inputform"] = dataSet1;
DataGrid1.EditItemIndex = -1;
bindGrid();
sqlConnection1.Close();
}
catch(Exception ex)
{
ErrorMessage.Text = ex.Message;
}

please help
 
J

Joe Mayo [C# MVP]

Janet Heflin said:
Very new in the C# world so give me a break here. I have created two
web forms, one passes information to the other and brings up the data
based on the passed field. When I do the updateRow I get the object
recrence not set to an instance of an object. If I rem out the error
message on the update it goes through and acts as though it updates
but does not. Here is the updateRow statements:

private void updateRow(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
try
{
ErrorMessage.Text = "";
SqlCommand updCommand = new SqlCommand();
updCommand.Connection = sqlConnection1;
updCommand.CommandText = "UPDATE HSE Compliance SET Date = '"
+ ((TextBox)e.Item.Cells[2].Controls[0]).Text + "', Description =
'" + ((TextBox)e.Item.Cells[3].Controls[0]).Text + "', Manager = '" +
((TextBox)e.Item.Cells[4].Controls[0]).Text + "', Risk-ranking = '" +
((TextBox)e.Item.Cells[5].Controls[0]).Text + "', Status = '" +
((TextBox)e.Item.Cells[6].Controls[0]).Text + "'"
+ "WHERE Ref_No = '" + ((TextBox)e.Item.Cells[1].Controls[0]).Text
+ "'";
Label2.Text = "Updating Ref No " +
((TextBox)e.Item.Cells[1].Controls[0]).Text;
updCommand.CommandType = CommandType.Text;
sqlConnection1.Open();
updCommand.ExecuteNonQuery();
sqlDataAdapter1.Fill(dataSet1);
Cache["hse_inputform"] = dataSet1;
DataGrid1.EditItemIndex = -1;
bindGrid();
sqlConnection1.Close();
}
catch(Exception ex)
{
ErrorMessage.Text = ex.Message;
}


Hi Janet,

Somewhere in your code, you are using an object that is still null (hasn't
been instantiated). For example, if you have the following object:

SqlConnection sqlConnection1 = null;

and then you try to access any of its members like this:

sqlConnection1.Open();

then a NullReferenceException exception will be thrown.

There are a couple ways to find out what the offending line is:

1. Step through the code in the debugger to see which line the exception is
thrown on.
2. Catch NullReferenceException and look at the stack trace to get the
method and line number in your code, using the ToString() method or the
StackTrace property.

Once you know where the exception is being thrown from, go back and see how
the objects being used on that line were initialized either by a code
walk-through or stepping through the code with the debugger.

Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top