![](/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.