[英]How can i fix Unable to cast object of type 'System.DBNull' to type 'System.String`
I get trying to get data using the following code 我尝试使用以下代码获取数据
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;
}
try 尝试
string value = row["BOM - RIM PN"].ToString();
In case you need some extra work for null values(maybe skip them) you need additional type check. 如果您需要为空值做一些额外的工作(可能跳过它们),则需要进行其他类型检查。
Check if it's DBNull
firstly and return an empty string or whatever is appropriate in your case. 首先检查它是否为DBNull
并返回一个空字符串或适合您的情况。 Otherwise call ToString()
on it. 否则,调用ToString()
。
var boxRim = row["BOM - RIM PN"];
string value = (bomRim == DBNull.Value) ? string.Empty : bomRim.ToString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.