本文介紹了可空對象必須有值嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
上線:bool travel = fill.travel.Value;
我收到以下錯誤:
On the line: bool travel = fill.travel.Value;
I am getting the following error:
可空對象必須有值
我不知道為什么.我想要做的就是獲取當前為假的旅行數據庫中的值.任何幫助,將不勝感激.
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
}
推薦答案
您可以隨時切換到
fill.travel.GetValueOrDefault()
提供默認值 (false) 或數據庫中布爾列的值.或者您可以使用重載指定默認值.無論哪種方式,可空對象當前都沒有值,這就是您得到該異常的原因.
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.
這篇關于可空對象必須有值嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!