473,322 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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
Nov 16 '05 #1
2 7619
"Janet Heflin" <ja**********@tdwilliamson.com> wrote in message
news:27**************************@posting.google.c om...
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
--
http://www.csharp-station.com
Nov 16 '05 #2
Did you instantiate sqlConnection1 in the code behind both forms?

Bern

"Janet Heflin" <ja**********@tdwilliamson.com> wrote in message
news:27**************************@posting.google.c om...
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

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Terrence | last post by:
I am doing some of the C# walkthroughs to transition from VB to C#. When I try to execute static void Main() { Aplication.Run(new Form1()) } I raise a 'System.NullReferenceException" in...
7
by: mike p. | last post by:
I have a docbook xml file, and am using standard docbook 1.61.3 xsl stylesheets to do xhtml transform. Transform works fine when using MSXML. When I try to do the following using asp.net 1.1: ...
0
by: muralidharan | last post by:
WebForm1.aspx Code: <%@ Register TagPrefix="ComponentArt" Namespace="ComponentArt.Web.UI" Assembly="ComponentArt.Web.UI" %> <ComponentArt:TreeView id="TreeView1" Height="520"...
5
by: Vitling | last post by:
For no apparent reason, a NullReference exception is thrown in system.dll (System.Net.Sockets.OverlappedAsyncResult.CompletionPortCallback). Since I only get a disassembly from Visual Studio, it...
2
by: Raed Sawalha | last post by:
i have a windows form(Main) with listview, when click an item in listview i open other window form (Sub) which generate the selected item from parent window in as treeview items when click any item...
1
by: msnews.microsoft.com | last post by:
I'm trying to fill an array of objects but when I add the first object I get a NullReferenceException. ----------------------------------------------------------------------------...
4
by: Terry Mulvany | last post by:
I have a 'BasePage' (BasePage.cs) derived from System.Web.UI.Page that all my pages inherit from. I need to set some properties (either in the OnInit or constructor) based on a potential...
2
by: sxiao | last post by:
Hi, there I got a NullReferenceException when there are more than one users trying to open the same page at the same time. The senerio is: Two users logged into the web application using the...
3
by: Brano | last post by:
HI all, I have a problem i have a web application that was working fine and this morning when i run it and click on a button that does Reponse.Redirect to a page i get this error : Server...
6
by: William Mild | last post by:
I must be getting brain fried. I can't see the error. Create a new web form with the following code begind: Public Class test Inherits System.Web.UI.Page Public Class ReportCardData ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.