![](/img/trans.png)
[英]System.InvalidCastException: Unable to cast object of type 'System.DBNull' to type 'System.String'
[英]How can i fix Unable to cast object of type 'System.DBNull' to type 'System.String`
我嘗試使用以下代碼獲取數據
無法將類型為“ System.DBNull”的對象轉換為類型為“ System.String”的對象
private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
{
if (e.IsGetData)
{
DataRow row = ((DataRowView)e.Row).Row as DataRow;
string value = (string)row["BOM - RIM PN"];
var results = from myRow in this.wHLMASTERDETAIL.RIMS.AsEnumerable()
where myRow.Field<string>("STOCK NO") == value
select myRow;
e.Value = results.ToList().Count;
}
嘗試
string value = row["BOM - RIM PN"].ToString();
如果您需要為空值做一些額外的工作(可能跳過它們),則需要進行其他類型檢查。
首先檢查它是否為DBNull
並返回一個空字符串或適合您的情況。 否則,調用ToString()
。
var boxRim = row["BOM - RIM PN"];
string value = (bomRim == DBNull.Value) ? string.Empty : bomRim.ToString();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.