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 
 }

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.


You are trying to access property from the nonexistent object (your fill.travel is null, and you calling prop from it), you can use coalesce operator (.Net 4.0):

bool travel = fill.travel ?? false;

You will get a InvalidOperationException if you access the Nullable.Value property when the HasValue property is false.


You can check if nullable variable has some value like this before your actually access its value

if(fill.travel.HasValue)
{
   bool travel = fill.travel.Value;
}

The value coming from the database is a nullable boolean. When you call Nullable.Value, and the value is null, you will get this exception. Consider checking the property Nullable.HasValue before calling .Value.