This is a migrated thread and some comments may be shown as answers.

Object Reference Error when using ExecuteScalar or ExecuteQuery

3 Answers 128 Views
Data Access Free Edition
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Steven
Top achievements
Rank 1
Steven asked on 04 May 2016, 07:40 PM

In our asp.net website project, we are receiving the following error when attempting to use context.ExecuteScalar or context.ExecuteQuery:

-------------------------------------------------------------------------------------------

Application Error:
-------------------------------------------------------------------------------------------
Object reference not set to an instance of an object.

   at Telerik.OpenAccess.Data.Common.OAConnection.SetupConverters(OADataReader oaDataReader)
   at Telerik.OpenAccess.Data.Common.OACommand.ExecuteReader(CommandBehavior behavior)
   at Telerik.OpenAccess.OpenAccessContextBase.ExecuteQuery[T](String commandText, CommandType commandType, DbParameter[] parameters)
   at PW.WEF.Web.WEFM001.Page_Load(Object sender, EventArgs a)
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
-------------------------------------------------------------------------------------------
Detailed Error:
-------------------------------------------------------------------------------------------
Object reference not set to an instance of an object.
-------------------------------------------------------------------------------------------
HTTP Error 500:
-------------------------------------------------------------------------------------------
Exception of type 'System.Web.HttpUnhandledException' was thrown.

System.Web

   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.ProcessRequest(HttpContext context)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

 

The queries causing this are not complex.  For example, here is one place that has caused this error:

public DateTime cp_db_date_time()
{
    string sQuery = "select SYSDATE from dual";
    return this.ExecuteScalar<DateTime>(sQuery);
}

This does not happen all the time.  I haven't been able to reproduce it locally, but when the production site starts to get hit hard during the busy parts of the day, this error crops up sporadically.  There are about 10 or so places where we use ExecuteQuery or ExecuteScalar and the error always occurs around one of these spots.  It does not occur any other time we access the database via linq queries and such.  

Any ideas as to why this is happening?

3 Answers, 1 is accepted

Sort by
0
Ralph Waldenmaier
Telerik team
answered on 06 May 2016, 12:56 PM
Hi Steven,
Thank you for contacting us.

The reported behavior indicates that the context in question was disposed already when the respective ExecuteScalar calls were done.

Are you trying to access the context instance from multiple threads? It could be that after obtaining the connection to execute the query the context is being disposed, probably by another thread. Since the OpenAccessContext is not thread safe it is recommended to use a context per thread. In case you need to access it from different threads, please set the "SupportConcurrentThreadsInScope" setting via the configuration.

To avoid such errors it is also recommended to manage the OpenAccessContext with the HttpContext. You can find more details here.

Again, such errors have to do with the disposal of the OpenAccessContext instance. Can you revisit your code to see if there are any potential issues indicating something in that area?

I hope this information is helpful for you.
Do come back in case you have any other question.

Regards,
Ralph Waldenmaier
Telerik
 
Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
0
Steven
Top achievements
Rank 1
answered on 06 May 2016, 06:42 PM

Thank you Ralph.  We are using the SupportConcurrentThreadsInScope, but we are not managing the OpenAccessContext with the HttpContext. I can't find anywhere that we would otherwise be disposing of the database context, so I think we may need to build some of the HttpContext stuff.

 

-Steven

0
Ralph Waldenmaier
Telerik team
answered on 10 May 2016, 06:53 AM
Hi Steven,
Yes using the HttpContext approach would be the recommendation in your setup. In general it is advisable to have a short lifetime of a context instance, to avoid such unwanted behavior.

Regarding the current situation. You say this happens only when the server gets hit hard. Probably this can be correlated to the recycling of the web application through the IIS, where a recycle is triggered and therefore the disposal is invoked but in the same time, concurrently, the ExecuteScalar method is invoked.
Also here the recommended approach would fix that since the context is only held as long as needed.

Feel free to ask in case you have any other question.

Regards,
Ralph Waldenmaier
Telerik
 
Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
Tags
Data Access Free Edition
Asked by
Steven
Top achievements
Rank 1
Answers by
Ralph Waldenmaier
Telerik team
Steven
Top achievements
Rank 1
Share this question
or