views:

89

answers:

3

Hi

I have a ASP.NET website. If I make a request for a page it works most of the times, but sometimes I get an HttpUnhandledException.

I have tried to log the errors, but from the errors messages I'm not able to solve the problem.

StackTrace:

at System.Web.UI.Page.HandleError(Exception e)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest()
   at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
   at System.Web.UI.Page.ProcessRequest(HttpContext context)
   at ASP.default_aspx.ProcessRequest(HttpContext context) in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\4e215a3c\72ef69da\App_Web_ylvnbciw.6.cs:line 0
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Data:

System.Collections.ListDictionaryInternal

BaseException:

System.InvalidOperationException: The connection was not closed. The connection's current state is open.
   at DbCategory.getParentCategories()
   at _Default.Page_Load(Object sender, EventArgs e)
   at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
   at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
   at System.Web.UI.Control.OnLoad(EventArgs e)
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

TargetSite:

Boolean HandleError(System.Exception)

I have idea that it's something about my session og get variables, but i'm not sure about that. Does anybody have an idea about what it could be?

+1  A: 

A good and common practice is to use Db objects (DbConnection, DbCommand, readers, adapters, etc.) in Using blocks.

This will prevent 99% of this type of thing from ever happening, by ensuring the objects are always disposed when you are done using them.

Tom Tresansky
A: 

Such errors are not because of wrong disposing - The connection was NOT closed.

Such error can be then you use SqlConnection in the static class or method which holds open connection, and after that - in not-static, and in this moment exception can occur.


Update: Well, this method is not very save - it do exactly that I was talking about - he creates connection, but never releases it. SqlConnection is very heavy object, and you should dispose it right after you get or set your data.

You should rewrite your logic - create one connection per page or per operation, and you should never store the connection in your classes.

If this is impossible for you, use the lock statement for ensure singleton, like this:

private static object singleton;

lock (singleton)
{
    // Some manipulations with your server
}
VMAtm
I've updated my answer, please review it
VMAtm
So you mean when all server operations has finished and the result has been returned to the users browser, the db connection is not released automatically?If so is there a method which automatically is called so I can release the connection when all server activity has finished?
jweber
1. You can use the .Close() or .Dispose() methods of the SqlConnection2. You can use the using statement: using (SqlConnection conn = new SqlConnection(DB_CONNECTION_STRING)) { // some code goes here }After all code from the using block is done, SqlConnection will be disposed automatically.
VMAtm
By using the "using(..){..}" I think my problems is solved. I haven't registreted any errors the last two days compared to about 20 per day before.Thank you very much.
jweber
Good luck in your projects
VMAtm
A: 

VMAtm: Maybe you got something there, because the exception is showing a lot more after the site is getting more and more users. I have a static class to hold the database connection using a singleton method...

public static SqlConnection getDbConnection() 
{ 
    if (sqlConn == null) 
    { 
        source = "data source=..."; 
        sqlConn = new SqlConnection(source); 
    }
    return sqlConn; 
}

Is this method completely save?

All database calls are called in abstract classes using static methods and these abstract classes are getting the db connection from the singleton method

jweber