抛出了类型为'System.Data.StrongTypingException'的异常 C#

3
if ((booking.BookingsVoucher[i].RedemptionReloc != null)). 

我在这里遇到了异常。在数据库中,RedemptionReloc的值为空。
Redemption TimeSttmp 
NULL 
Redemption Reloc 
NULL 
RedeemedCurrencyC 
NULL 
Redeemed Amo_ 
NULL 

尝试使用DBNULL.value.equal(booking.BookingsVoucher[i].RedemptionReloc)。但这也会抛出异常。

添加凭证详情时发生异常:System.Data.StrongTypingException: 表“BookingsVoucher”中列“RedemptionReloc”的值为DBNull。 ---> System.InvalidCastException: 无法将类型为“System.DBNull”的对象强制转换为类型“System.String”。


我们需要更多信息。BookingsVoucher 是某种类型的集合。那是什么类型,它是如何定义的? 你使用什么来将数据从数据库获取到该类型中?ADO.NET 和数据表?实体框架?自定义解决方案? - seangwright
1个回答

10

enter image description here

将NullValue项更改为Null或Empty,并相应地进行检查。

DataSet1 ds1 = new DataSet1();

        SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ContosoUniversity2ConnectionString"].ConnectionString);

        using (cn)
        {
            using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Department", cn))
            {
                adapter.Fill(ds1.Department);
                foreach (DataSet1.DepartmentRow row in ds1.Department.Rows)
                {
                    if (row.Name != null)
                    {
                        Console.WriteLine(row.Name);
                    }

                }
            }
        }

您正在使用哪种数据访问技术,强类型数据集还是实体框架(EF)? - Sree Harsha Nellore

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接