Exception from SqlConnection.Close

  • Thread starter Thread starter TooCool
  • Start date Start date
T

TooCool

Exception from SqlConnection.Close

"Object reference not set to an instance of an object"

Web app running on a web farm

..NET 1.1 / ASP.NET / ADO.NET

Exception occurs approximately 4 times per day.

Extensive single web-client testing did not reveal this exception.

Does this particular exception indicate a bug within the SqlConnection
class?

Any suggestion about how to deal with it?

--Larry
 
I would say this is caused by someone using Dispose instead of Close on the
object. Dispose dumps the object after closing it.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
Do you check that the connection object itself is not null before calling
close? If so, and you're getting a nullref exception from SqlClient, do you
see the stack trace at the point of the exception or can you capture one? If
you do, it would be great if you could post it here; I can take a look at
try to figure out what's going on.

--
Pablo Castro
Program Manager - ADO.NET Team
Microsoft Corp.

This posting is provided "AS IS" with no warranties, and confers no rights.
 
I was able to simulate the error.



Here is the piece of code:



private void SimulateConnectionError()
{
SqlConnection conn = new SqlConnection("Initial Catalog=SFSQL_TCIM;Data
Source=WTSCHLFM;Integrated Security=SSPI");
conn.Open();
SqlTransaction transaction =
conn.BeginTransaction(IsolationLevel.Serializable, "LOCAL_TCIM");
SqlCommand command = new SqlCommand("select * from assembly", conn,
transaction);
SqlDataReader reader = command.ExecuteReader();
reader.Read();
reader.Close();
Thread.Sleep(10000);
conn.Close();
}



After clicking on the button that is executing this method, and about after
1-2 seconds unplug the network cable and here it is.

So, the network should be down after getting the DataReader and before
trying to close the connection.



Here is the exception:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

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

Source Error:

Line 60: reader.Close();Line 61:
Thread.Sleep(10000);Line 62: conn.Close();Line
}Line 64:


Source File: c:\documents and settings\all
users\webdocs\wwwroot\webapplication1\webform1.aspx.cs Line: 62

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an
object.] System.Data.SqlClient.SqlConnection.Close()
WebApplication1.WebForm1.SimulateConnectionError() in c:\documents and
settings\all users\webdocs\wwwroot\webapplication1\webform1.aspx.cs:62
WebApplication1.WebForm1.Button1_Click(Object sender, EventArgs e) in
c:\documents and settings\all
users\webdocs\wwwroot\webapplication1\webform1.aspx.cs:67
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePo
stBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,
String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()
 
I was able to simulate the error.



Here is the piece of code:



private void SimulateConnectionError()
{
SqlConnection conn = new SqlConnection("Initial Catalog=SFSQL_TCIM;Data
Source=WTSCHLFM;Integrated Security=SSPI");
conn.Open();
SqlTransaction transaction =
conn.BeginTransaction(IsolationLevel.Serializable, "LOCAL_TCIM");
SqlCommand command = new SqlCommand("select * from assembly", conn,
transaction);
SqlDataReader reader = command.ExecuteReader();
reader.Read();
reader.Close();
Thread.Sleep(10000);
conn.Close();
}



After clicking on the button that is executing this method, and about after
1-2 seconds unplug the network cable and here it is.

So, the network should be down after getting the DataReader and before
trying to close the connection.



Here is the exception:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

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

Source Error:

Line 60: reader.Close();Line 61:
Thread.Sleep(10000);Line 62: conn.Close();Line
}Line 64:


Source File: c:\documents and settings\all
users\webdocs\wwwroot\webapplication1\webform1.aspx.cs Line: 62

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an
object.] System.Data.SqlClient.SqlConnection.Close()
WebApplication1.WebForm1.SimulateConnectionError() in c:\documents and
settings\all users\webdocs\wwwroot\webapplication1\webform1.aspx.cs:62
WebApplication1.WebForm1.Button1_Click(Object sender, EventArgs e) in
c:\documents and settings\all
users\webdocs\wwwroot\webapplication1\webform1.aspx.cs:67
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePo
stBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,
String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()
 
I was able to simulate the error.

Here is the piece of code:

private void SimulateConnectionError()
{
SqlConnection conn = new SqlConnection("Initial Catalog=SFSQL_TCIM;Data
Source=WTSCHLFM;Integrated Security=SSPI");
conn.Open();
SqlTransaction transaction =
conn.BeginTransaction(IsolationLevel.Serializable, "LOCAL_TCIM");
SqlCommand command = new SqlCommand("select * from assembly", conn,
transaction);
SqlDataReader reader = command.ExecuteReader();
reader.Read();
reader.Close();
Thread.Sleep(10000);
conn.Close();
}

After clicking on the button that is executing this method, and about after
1-2 seconds unplug the network cable and here it is.
So, the network should be down after getting the DataReader and before
trying to close the connection.

Here is the exception:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

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

Source Error:
Line 60: reader.Close();
Line 61: Thread.Sleep(10000);
Line 62: conn.Close();
Line 63: }
Line 64:

Source File: c:\documents and settings\all
users\webdocs\wwwroot\webapplication1\webform1.aspx.cs Line: 62

Stack Trace:
[NullReferenceException: Object reference not set to an instance of an
object.]
System.Data.SqlClient.SqlConnection.Close()
WebApplication1.WebForm1.SimulateConnectionError() in c:\documents and
settings\all users\webdocs\wwwroot\webapplication1\webform1.aspx.cs:62
WebApplication1.WebForm1.Button1_Click(Object sender, EventArgs e) in
c:\documents and settings\all
users\webdocs\wwwroot\webapplication1\webform1.aspx.cs:67
System.Web.UI.WebControls.Button.OnClick(EventArgs e)

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePo
stBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()

--Larry
 
We have a simple test program that will reproduce this
NullReferenceException if the Network cable is unplugged after a DataReader
operation but before a SqlConnection.Close operation. We wonder if a Network
timeout might produce a similar condition?



In our web app this NullReferenceException occurs in a finally block as
follows:



SqlConnection conn = null;

try

{

conn = new SqlConnection(TcimDbTable.GetConnectionString());

o

o

o

}

catch( SqlException e)

{

o

o

o

}

finally

{

if (conn != null)

conn.Close();

o

o

o

}



The web error page reports the stack trace as follows:



Object reference not set to an instance of an object.

at System.Data.SqlClient.SqlConnection.Close()

at
StateFarm.Application.W0081894.Business.ComponentGroupControl.GetUserPermiss
ion(String componentGroupCode) in ---
C:\Projects\TCIM\TCIMSolution\TcimApp\Business\ComponentGroupControl.cs:line
112

at .

o

o

o



--Larry
 
We have a simple test program that will reproduce this
NullReferenceException if the Network cable is unplugged after a DataReader
operation but before a SqlConnection.Close operation. We wonder if a Network
timeout might produce a similar condition?



In our web app this NullReferenceException occurs in a finally block as
follows:



SqlConnection conn = null;

try

{

conn = new SqlConnection(TcimDbTable.GetConnectionString());

o

o

o

}

catch( SqlException e)

{

o

o

o

}

finally

{

if (conn != null)

conn.Close();

o

o

o

}



The web error page reports the stack trace as follows:



Object reference not set to an instance of an object.

at System.Data.SqlClient.SqlConnection.Close()

at
StateFarm.Application.W0081894.Business.ComponentGroupControl.GetUserPermiss
ion(String componentGroupCode) in ---
C:\Projects\TCIM\TCIMSolution\TcimApp\Business\ComponentGroupControl.cs:line
112

at .

o

o

o



--Larry
 
We have a simple test program that will reproduce this
NullReferenceException if the Network cable is unplugged after a DataReader
operation but before a SqlConnection.Close operation. We wonder if a Network
timeout might produce a similar condition?



In our web app this NullReferenceException occurs in a finally block as
follows:



SqlConnection conn = null;

try

{

conn = new SqlConnection(TcimDbTable.GetConnectionString());

o

o

o

}

catch( SqlException e)

{

o

o

o

}

finally

{

if (conn != null)

conn.Close();

o

o

o

}



The web error page reports the stack trace as follows:



Object reference not set to an instance of an object.

at System.Data.SqlClient.SqlConnection.Close()

at
StateFarm.Application.W0081894.Business.ComponentGroupControl.GetUserPermiss
ion(String componentGroupCode) in ---
C:\Projects\TCIM\TCIMSolution\TcimApp\Business\ComponentGroupControl.cs:line
112

at .

o

o

o



--Larry
 
Anything that makes the pooled connection invalid will cause this (or
similar) exceptions. Once you try to open or use the pooled connection after
it's "broken" you'll see exceptions. Once the exception is thrown, the
pooling mechanism knows to flush that specific connection--even though all
of the others might be (are probably) bad as well. Until ADO 2.0 you'll have
to cycle through the bad connections until you get all of them flushed out
or kill the process that owns the connection pool.

hth

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
William (Bill) Vaughn said:
I would say this is caused by someone using Dispose instead of Close on the
object. Dispose dumps the object after closing it.

No it doesn't. It may cause ObjectDisposedException to be thrown when
you try to use the object again, but it certainly doesn't cause the
object to be destroyed or anything like that.
 
Sorry for repeats. An Outlook Express error said that message wasn't sent.

Please see the previous reply for additional information.

--Larry
 
Back
Top