tags:

views:

165

answers:

2

I am trying to achieve the following...

 _4S.NJB_Request request =
                (from r in db.NJB_Requests
                 where r.RequestId == referenceId
                 select r).Take(1).SingleOrDefault();

Getting the following exception...

    Message:
The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type.


StackTrace:
   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.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
   at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source)
   at DAL.SqlDataProvider.MarkNJBPCRequestAsComplete(Int32 referenceId, Int32 processState)

I have verified that 'referenceId' does have a value.

Anyone know why this would happen in a select statement?

Thanks!

EDIT: r.RequestId = NOT NULL Int, referenceId = int

A: 

I think the Take call is redundant and keeping the SingleOrDefault from saving you from the exception, try replacing Take(1).SingleOrDefault with FirstOrDefault:

(from r in db.NJB_Requests
                 where r.RequestId == referenceId
                 select r).FirstOrDefault();

edit: changed SingleOrDefault to FirstOrDefault to avoid getting an exception if more than 1 item is returned.

kekekela
That will throw an expcetion if there are more that 1 element in the list. Instead you would use FirstOrDefault()
Oskar Kjellin
"That will throw an expcetion if there are more that 1 element in the list." -- Then use FirstOrDefaultedit: Heh, I see you updated your comment to make it appear that you suggested FirstOrDefault initially...updating mine to show how it read when I replied.
kekekela
thiag0
Actually I edited my comment and when I got back from editing I saw that you had replied allready ;)thiag0 check my other answer
Oskar Kjellin
What type is referenceId and what type is Request.ID...might want to just edit that into the OP since it looks like someone else already asked the same thing in the comments up there.
kekekela
A: 

Is r.RequestId nullable? Then you would use:

 _4S.NJB_Request request =
                (from r in db.NJB_Requests
                 where r.RequestId == (int?)referenceId
                 select r).FirstOrDefault();

Are your dc up to date with the database? Perhaps you have a null in the database that is being assigned to an int becuase your model is not synced with the database

Oskar Kjellin
@Kurresmack - RequestId is not nullable as it is the Primary Key for the table
thiag0
Are your dc up to date with the database? Perhaps you have a null in the database that is being assigned to an int becuase your model is not synced with the database
Oskar Kjellin
@Kurresmack - looks like my local db was not in sync with the production db..thanks for your help sir..
thiag0
No problem, nice to hear that it worked out!
Oskar Kjellin