Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

On the line: bool travel = fill.travel.Value; I am getting the following error:

Nullable object must have a value

and i am not sure why. All I want to do is get the value in the database of travel which is currently false. Any help would be appreciated.

using (var db = new DataClasses1DataContext())
{
    var fill = (from f in db.expenseHdrs
                where f.rptNo == getPkRowReport()
                select f).FirstOrDefault();

    txtReportDesc.Text = fill.description;
    txtPeriod.Text = fill.period;
    txtPurpose.Text = fill.purpose;

    bool travel = fill.travel.Value;
    chkTravel.Checked = travel 
 }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
114 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can always switch to

 fill.travel.GetValueOrDefault()

To provide the default (false), or the value of the boolean column from the database. Or you can specify the default with an overload. Either way, the nullable currently doesnt have a value, which is why you get that exception.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...