none
NullReferenceException at SqlProvider.Execute ... at SingleOrDefault RRS feed

  • Question

  • Hi,

    It is happening occasionally but I am receiving exception reports from our client pc's when our application is trying to query an entity by it's ID from database:

    public void IsLoginValid(...
    {
    using (var db = new Database()) { var dbE = db.Employees.SingleOrDefault(e => e.Employee_Id == Employee_Id);
    ...

    Database here is a class derived from DataContext.

    The code is located inside Entity class Employee (assosiated with Table) and it is executed on the main UI thread of wpf application.

    Employee_Id is defined as following:

    [Column(IsPrimaryKey = true, IsDbGenerated = true)]
    public int Employee_Id { get; set; }

    How is it possible to get exception from SingleOrDefault here?

    The part of stack trace:

    System.NullReferenceException: Object reference not set to an instance of an object.
       at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
       at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
       at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
       at System.Data.Linq.Table`1.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
       at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source, Expression`1 predicate)
       at GPFC.ROIPOS.Models.Employee.IsLoginValid
    Thanks in advance for your help.





    • Edited by GPAndrei Tuesday, October 13, 2015 6:54 PM
    Tuesday, October 13, 2015 6:02 PM

All replies

  • How is it possible to get exception from SingleOrDefault here?

    It's expected and was documented on MSDN. https://msdn.microsoft.com/en-us/library/vstudio/bb342451(v=vs.100).aspx

    If the ID cannot be found in database, SingleOrDefault method will return default value of class. In this case, the default value of Employee class is null. So when you read property of Employee instance, app throw null reference exception to you. Try the following code snippet to work around this scenario.

    using (var db = new Database())
                {
                    var dbE = db.Employees.SingleOrDefault(e => e.Employee_Id == Employee_Id);
                    if (dbE == null)
                    {
                        //cannot find related record according to ID
                    }
                    else
                    {
                        //find realted record
                    }
                }

    I hope it helps.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, October 14, 2015 3:18 AM
    Moderator
  • Thank you for your reply.

    The exception is occuring exactly on the line, not after it. Next line contains a check, so it returns false if dbE is null:

    public bool IsLoginValid(...
    {
        using (var db = new Database())
        {
            var dbE = db.Employees.SingleOrDefault(e => e.Employee_Id == Employee_Id);
            if (dbE == null || dbE.IsAccountLocked || dbE.IsPasswordExpired) 
                return false;
    
    SingleOrDefault should return null, it should not throw exception if there is no such entity in the database, right?

    Wednesday, October 14, 2015 1:33 PM
  • SingleOrDefault should return null, it should not throw exception if there is no such entity in the database, right?

    Yes. There are two exceptions defined in SingleOrDefualt method, but not contains Null reference exception. So I don't know why it occasionally throws that exception according the available code snippet.

    I suggest you change to use Where or FirstOrDefault method as a work around, and try provide a repro project to me for future troubleshooting. You can upload it using your OneDrive and share a link here.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 16, 2015 3:05 AM
    Moderator
  • Thank you for reply. 

    It is happening not only in SingleOrDefault as I mentioned it in https://social.msdn.microsoft.com/Forums/en-US/d936491e-c3b4-4b36-86a4-9a7e41c36132/unable-to-cast-systemobject-to-type-systemsecurityipermission-at?forum=linqtosql.

    So far, I have not been able to reproduce the issues. They are still occurring on client machines.

    I may be wrong but it seems to me that the problem is occurring as a consequence of memory corruption, about which Martin Xie wrote in his answer in https://social.msdn.microsoft.com/forums/vstudio/en-us/4f48c152-68cd-45ec-a11e-baa7de7f79c3/attempted-to-read-or-write-protected-memory.



    GPAndrei





    • Edited by GPAndrei Wednesday, November 4, 2015 1:32 PM
    Wednesday, November 4, 2015 1:23 PM