【问题标题】:threw an exception of type 'System.Data.StrongTypingException' C#抛出了“System.Data.StrongTypingException”C# 类型的异常
【发布时间】:2016-11-10 13:14:00
【问题描述】:
if ((booking.BookingsVoucher[i].RedemptionReloc != null)). 

我在这里遇到异常。 RedemptionReloc 在 dbase 中有空值

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?实体框架?定制解决方案?

标签: c# exception


【解决方案1】:

将 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?
猜你喜欢
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 2013-08-17
  • 1970-01-01
  • 2021-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多