简体   繁体   English

如何修复无法将类型为“ System.DBNull”的对象转换为类型为“ System.String”的对象

[英]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.

相关问题 System.InvalidCastException:无法将类型为System.DBNull的对象转换为类型为System.String的对象 - System.InvalidCastException: Unable to cast object of type 'System.DBNull' to type 'System.String' 转换:无法将“System.DBNull”类型的对象转换为“System.String”类型 - Transformation: Unable to cast object of type 'System.DBNull' to type 'System.String' RowDataBound:从dataTable获取值! 无法将“System.DBNull”类型的对象强制转换为“System.String”类型 - RowDataBound : Getting value from dataTable ! Unable to cast object of type 'System.DBNull' to type 'System.String' 无法将“System.DBNull”类型的对象转换为 Scaffold-DbContext .Net Core 中的“System.String”类型 - Unable to cast object of type 'System.DBNull' to type 'System.String' in Scaffold-DbContext .Net Core Entity Framework Group BY 导致无法将“System.DBNull”类型的对象转换为“System.String”类型 - Entity Framework Group BY causing Unable to cast object of type 'System.DBNull' to type 'System.String' 获取所有产品时无法将类型为“System.DBNull”的 object 转换为类型“System.String” - Unable to cast object of type 'System.DBNull' to type 'System.String' On getting all Products 未以根身份连接时,无法将类型为“ System.DBNull”的对象转换为类型为“ System.String”的对象 - Unable to cast object of type 'System.DBNull' to type 'System.String' WHEN NOT CONNECTING AS ROOT 无法将“System.DBNull”类型的 object 转换为类型“System.String” - Unable to cast object of type 'System.DBNull' to type 'System.String` 如果默认情况下字符串为空,为什么我无法将类型为“ System.DBNull”的对象转换为类型为“ System.String”的对象? - If strings are nullable by default, why do i get Unable to cast object of type 'System.DBNull' to type 'System.String'.? 如何修复异常:无法将“System.DBNull”类型的 object 转换为“System.Byte []”类型 - How to fix exception:Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM