Tom Gullen
Tom Gullen

Reputation: 61775

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

Running this code. Works fine on local dev server, but not on live server.

try
{
    var qq =
    db.tblConstructReleaseDownloads
    .Where(c => c.UserID.HasValue && c.UserID.Value == userID)
    .OrderBy(c => c.DateStarted)
    .ThenBy(c => c.ReleaseID);

    var query = db.GetCommand(qq).CommandText;
    HttpContext.Current.Response.Write(query + "<br><br>");

    foreach (var d in qq)
    {
        HttpContext.Current.Response.Write("ID:" + d.ID + "<br>");
    }

This throws the error:

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

The command text it prints out is:

SELECT     ID, ReleaseID, IP, DateCompleted, BytesServed, UserID, DateStarted, BytesPerSecond, TrackerVisitID
FROM         tblConstructReleaseDownloads AS t0
WHERE     (UserID IS NOT NULL) AND (UserID = @p0)
ORDER BY DateStarted, ReleaseID

I run this query on the live database and it works fine, no errors.

Anyone got any ideas what is causing this?

Upvotes: 2

Views: 6159

Answers (2)

CoolPapaCash
CoolPapaCash

Reputation: 11

If you are using MVC check your model. I wrestled with this for about 3 hours before I checked the model and changed the int value to Nullable int. Works like a charm now.

Upvotes: 1

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174457

One of the columns returned by the query contains a NULL value in the database on the live server but not on the dev server.
Because the corresponding property in your domain model is defined as int and not int? you should mark that column as not nullable.

Upvotes: 7

Related Questions