CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2001
    Location
    Denmark
    Posts
    453

    Question nullable variables not working on an IIS???

    I have a function

    Code:
           public List<LabelCreatorDataClasses.LabelLog> GetLabelLogData(States ?state)
            {
                try
                {
                    List<LabelCreatorDataClasses.LabelLog> list = new List<LabelCreatorDataClasses.LabelLog>();
                    IQueryable<LabelLog> rows;
                    if (state!=null && state.HasValue)
                        rows = entityFramework.LabelLog.Where(r => r.Status == (short)state.Value);
                    else
                        rows = entityFramework.LabelLog.Where(r => true);
    
    .....
    In my webapp, I call it with
    m_list = m_dal.GetLabelLogData(null);

    When I test with the built-in webserver (that comes with VS2012), everything is fine.

    But when I deploy to an IIS, I get:
    InvalidOperationException: Nullable object must have a value.

    I've tried to add a value rather than null, and then everything is fine.

    I hope someone can shed some light over this...

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: nullable variables not working on an IIS???

    This line is redundant:
    if (state!=null && state.HasValue)
    All you need is:
    if (state.HasValue)
    Is r.State nullable (I assume it isn't)?

    You don't specify which line is failing, but try rewriting
    if (state!=null && state.HasValue)
    rows = entityFramework.LabelLog.Where(r => r.Status == (short)state.Value);
    else
    rows = entityFramework.LabelLog.Where(r => true);
    to ...
    rows = entityFramework.LabelLog.Where(r => r.Status == (state.HasValue ? (short)state.Value : r.Status));
    I would step through it in a debugger and see exactly where it's failing.

  3. #3
    Join Date
    Jun 2001
    Location
    Denmark
    Posts
    453

    Re: nullable variables not working on an IIS???

    I believe it is the call to the function that fails, although it places itself on the } that closes the function. Everything in the function is in a try-catch.

    But the fun thing is really, that while using the Debug setup and running on my own machine everything works without a hitch. But when I use the deployment configuration (not Release, it was actually made as a copy of Debug), the function call fails with the above mentioned message.

    I can't figure out what the difference between those two configurations is. Are there some settings in a project that would not allow

    void f(<sometype>? x)

    and then a call f(null); ?

  4. #4
    Join Date
    Apr 2014
    Location
    in northeast ohio
    Posts
    94

    Re: nullable variables not working on an IIS???

    im not so great with server stuff
    http://msdn.microsoft.com/en-us/libr...vs.120%29.aspx
    Use the Nullable<T>.GetValueOrDefault method to return either the assigned value,
    or the default value for the underlying type if the value is null, for example
    int j = x.GetValueOrDefault();
    Use the HasValue and Value read-only properties to test for null and retrieve the value,
    as shown in the following example:
    if(x.HasValue) j = x.Value;

    The HasValue property returns true if the variable contains a value, or false if it is null.
    The Value property returns a value if one is assigned. Otherwise, a System.InvalidOperationException is thrown.
    note the wording if one is assigned
    so basically your assigning a nullable with a value that is null to a non nullable type without checking properly via HasValue
    or and then not using GetValueOrDefault in the case were you want to overide the null
    so its probably your boolean logic
    break it up
    Last edited by willmotil; June 6th, 2014 at 03:53 AM.

  5. #5
    Join Date
    Jun 2001
    Location
    Denmark
    Posts
    453

    Re: nullable variables not working on an IIS???

    Thanks.

    I found the error. After adding a bucketload of log statements, I found out that some of the data in a following foreach loop where I fetch data from the table was actually null in the production database.
    The system just decided to lie to me as to what line was at fault.

    But thanks for the msdn article anyway - it might certainly be useful in the future :-)

  6. #6
    Join Date
    Apr 2014
    Location
    in northeast ohio
    Posts
    94

    Re: nullable variables not working on an IIS???

    ah good i was wrong about the null != x that does work
    int? x = null;
    if (x.HasValue){ Console.WriteLine("it has value"); }
    else{ Console.WriteLine("it doesnt have a value"); }
    if (x != null) { Console.WriteLine("its not null " + x.ToString()); int y = (int)x; }
    if (x == null) { Console.WriteLine("its null"); }
    //int y = (int)x; //throws the error

    maybe wrap it with a if else and get default value like in there example but throw a messege to the user or log the error
    you might want to use it on a production database with known errors and be able to log all the null values in it
    Last edited by willmotil; June 6th, 2014 at 03:59 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured