【问题标题】:Object cannot be cast from DBNull to other types for Double Data Type对象不能从 DBNull 转换为 Double 数据类型的其他类型
【发布时间】:2013-11-27 08:16:17
【问题描述】:

当任何单元格的数据库中有空值时,我看到错误,无法将对象从 DBNull 转换为其他类型。 - Asp.net 网格视图

<asp:TemplateField ItemStyle-Width="120" HeaderText="Price Difference">
                                     <ItemTemplate>
<%# PercentageChange(DataBinder.Eval(Container.DataItem, "FirstPrice"),DataBinder.Eval(Container.DataItem, "SecondPrice")) %>

                                     </ItemTemplate>
                                 </asp:TemplateField>

C#

protected string PercentageChange(object client_Price, object second_price)
    {
       double price1 = Convert.ToDouble(client_Price);
            double price2 = Convert.ToDouble(second_price);
            double percentagechange = ((price1 - price2) / price2) * 100;
             return percentagechange ;
} 

【问题讨论】:

  • DBNull,我有多讨厌你?让我数一数……stackoverflow.com/a/9632050/23354
  • 我确实有另一个问题,假设 price1 是 dbml 表的属性,标记为位并放置在 datagridview 的第 7 列。 bool canViewInOut = (dataGridView1.Rows[i].Cells[ 7].Value == DBNull.Value || !Convert.ToBoolean(dataGridView1.Rows[i].Cells[7].Value)) ?假:真;

标签: c# asp.net


【解决方案1】:

您需要将您的值与DBNull.Value 进行比较,如下所示

protected string PercentageChange(object client_Price, object second_price)
{
       if(client_price==DBNull.Value)
       {
           .....
       }
       //double price1 = Convert.ToDouble(client_Price);
       //double price2 = Convert.ToDouble(second_price);
       //double percentagechange = ((price1 - price2) / price2) * 100;
       //return percentagechange ;
 } 

【讨论】:

    【解决方案2】:

    然后检查是DBNull还是null:

    protected string PercentageChange(object client_Price, object second_price)
    {
        if(DBNull.Value.Equals(client_Price) || client_Price == null || DBNull.Value.Equals(second_price) || second_price == null)
            return "N/A"; // or whatever
    
        double price1 = Convert.ToDouble(client_Price);
        double price2 = Convert.ToDouble(second_price);
        double percentagechange = ((price1 - price2) / price2) * 100;
        return percentagechange.ToString();
    } 
    

    【讨论】:

    • 只是观察性的,但client_Price is DBNull 可能是一种更方便的测试方式(而不是DBNull.Value.Equals(client_Price) - 但这个答案应该可以正常工作
    • @MarcGravell 将 client_price==DBNull.Value 不起作用?
    • @Shekhar 这将是一个引用相等检查,所以是的,它会在一些非常邪恶的恶意示例中工作除了,如果你真的想要,我可以创建这些示例,但是不应影响实际使用
    • @MarcGravell 我很想看看这个例子:)
    • @Shekhar 可能涉及FormatterServices.GetUninitializedObject;真正的边缘案例
    【解决方案3】:

    错误原因: 在面向对象的编程语言中,null 表示没有对对象的引用。 DBNull 表示未初始化的变体或不存在的数据库列。 来源:MSDN

    我遇到错误的实际代码:

    改代码前:

            if( ds.Tables[0].Rows[0][0] == null ) //   Which is not working
    
             {
                    seqno  = 1; 
              }
            else
            {
                  seqno = Convert.ToInt16(ds.Tables[0].Rows[0][0]) + 1;
             }
    

    改代码后:

       if( ds.Tables[0].Rows[0][0] == DBNull.Value ) //which is working properly
            {
                        seqno  = 1; 
             }
                else
                {
                      seqno = Convert.ToInt16(ds.Tables[0].Rows[0][0]) + 1;
                 }
    

    结论: 当数据库值返回空值时,我们建议使用 DBNull 类,而不是像 C# 语言那样只指定为空。

    【讨论】:

      猜你喜欢
      • 2011-08-31
      • 1970-01-01
      • 1970-01-01
      • 2018-12-21
      • 1970-01-01
      相关资源
      最近更新 更多