SqlSessionState: Unable to cast object of type System.DBNull to type System.Byte[]

383 Views Asked by At

Occasionally we get the following error. I have not been able to reproduce it. I've tried logging out and navigating to a page that requires authentication, but it doesn't throw this exception.

So far this exception has only occurred on web services that are called immediately after page load. (So page load occurs, then an onready event starts loading some data with ajax calls)

We are using web forms with a SQL server session store.

Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.
at System.Web.SessionState.SqlSessionStateStore.DoGet(HttpContext context, String id, Boolean getExclusive, Boolean& locked, TimeSpan& lockAge, Object& lockId, SessionStateActions& actionFlags) at System.Web.SessionState.SqlSessionStateStore.GetItemExclusive(HttpContext context, String id, Boolean& locked, TimeSpan& lockAge, Object& lockId, SessionStateActions& actionFlags) at System.Web.SessionState.SessionStateModule.GetSessionStateItem() at System.Web.SessionState.SessionStateModule.BeginAcquireState(Object source, EventArgs e, AsyncCallback cb, Object extraData) at System.Web.HttpApplication.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

What can I do to fix this?

1

There are 1 best solutions below

0
On

Sounds like your session is timing out yet you are assuming you are retrieving the object from the session state.

I suggest adding a null check to the code before attempting to assign the session object.