none
Nullable object error.....Urgent !!! RRS feed

  • Question

  •  Here CreatedTime is returning null value.

    var RT =    from Time in context.User
                     where Time.UserId.Equals("Kamlesh")
                     select Time.CreatedTime;

    string DateRT = RT.First().Value.ToString();----------->this line is  throwing error as Nullable object must have a value.

    Also CreatedTime is of type smalldatetime.

    Kindly help as soon as possible.

     

     


    Monday, September 26, 2011 10:29 AM

Answers

  • Hi,

    This is what happens when you try to access the Value property of a nullable value that doesn't have a value. So CreatedTime for this line is likley NULL in your db.

    You could use d.HasValue?d.Value.ToString():"" to return the date as a string if defined and an empty string otherwise (or differ this until binding to the UI)...


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    • Proposed as answer by Kris444 Monday, September 26, 2011 3:21 PM
    • Marked as answer by Larcolais Gong Wednesday, October 5, 2011 5:04 AM
    Monday, September 26, 2011 12:19 PM

All replies

  • Hi,

    This is what happens when you try to access the Value property of a nullable value that doesn't have a value. So CreatedTime for this line is likley NULL in your db.

    You could use d.HasValue?d.Value.ToString():"" to return the date as a string if defined and an empty string otherwise (or differ this until binding to the UI)...


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    • Proposed as answer by Kris444 Monday, September 26, 2011 3:21 PM
    • Marked as answer by Larcolais Gong Wednesday, October 5, 2011 5:04 AM
    Monday, September 26, 2011 12:19 PM
  • Hi, 

    the First() object you are returning appears to be Null, when returning nullable objects it's always a good idea to see obj.HasValue property first and then get the obj.Value

     

    Regards


    Clarity VS Precision
    Monday, September 26, 2011 6:05 PM