【问题标题】:Object cannot be cast from DBNull to other types error shown?对象不能从 DBNull 转换为其他类型的错误显示?
【发布时间】:2013-11-22 12:15:25
【问题描述】:

我的代码是这样的

    private MyCatch _catch = new MyCatch("Description");

    decimal getTotalValue(GridView view, int listSourceRowIndex)
    {
        decimal unitPrice = Convert.ToDecimal(view.GetListSourceRowCellValue(listSourceRowIndex, "Each"));  //Object cannot be cast from DBNull to other types
        decimal quantity = Convert.ToDecimal(view.GetListSourceRowCellValue(listSourceRowIndex, "Quantity"));
        decimal discount = Convert.ToDecimal(view.GetListSourceRowCellValue(listSourceRowIndex, "TaxPercentage"));
        return unitPrice * quantity * (1 - discount);
    }

    private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
    {
        GridView view = sender as GridView;
        if (e.Column.FieldName == "Totalototal" && e.IsGetData) e.Value =
          getTotalValue(view, e.ListSourceRowIndex);
    }

【问题讨论】:

    标签: c# winforms devexpress


    【解决方案1】:

    这里我假设,如果为 Null,它将 unitPrice 设置为 0。

    decimal unitPrice = view.GetListSourceRowCellValue(listSourceRowIndex, "Each") == DBNull.Value
                                    ? 0
                                    : Convert.ToDecimal(view.GetListSourceRowCellValue(listSourceRowIndex, "Each"));
    

    【讨论】:

    • 嗨,奈尔,非常感谢
    【解决方案2】:

    如果可以有空值,请尝试使用可空类型。

    decimal? unitPrice = ...
    

    可空类型是一种接受空值作为值的值类型。然后您可以检查值

    if (unitPrice.HasValue) // is there a value or null?
        unitPrice.Value // do something with the value
    

    MSDN 上了解有关可空值的更多信息。

    但我假设不应该收到 null,这会使计算变得不可能。因此,我建议将获取值封装在 try/catch 块中,并在某些调用引发异常时从方法返回。

    【讨论】:

    • 嗨 Ondrej,可为空意味着如何?我是 C# 新手,请帮帮我。
    • @SriHari 我会解释的。
    • @SriHari ..请看下面我的回答
    • 您好 Ondrej,谢谢我对 DBNull 有一个想法
    【解决方案3】:

    问题在于,从中获取的数据值可能包含 DBNull 值,这意味着该值不存在。

    所以,改变不存在的值的类型是没有意义的。

    允许 null 的解决方案是将变量声明为可为 null 的类型,这意味着它们能够接受 null 值。

    语法是:

    datatype? variablename
    

    这有帮助..

    【讨论】:

      【解决方案4】:

      您可以使用TryParse 方法来确定是否可以从给定值转换为十进制。如果不可能,请分配DBNull.Value

      语法: decimal.TryParse(value,out parameter)

      如果值可以强制转换为decimal,则上述函数返回true
      无法投射时返回false

      当您需要将Null 插入表格列时,您应该从代码中插入DBNull.Value。 因此,当无法进行投射时,您可以发送DBNull.Value

      注意:这里我使用了三元 ?: 运算符将整个内容写在一行中
      解决方案:

       decimal result;
       decimal unitPrice =(decimal.TryParse(view.GetListSourceRowCellValue(listSourceRowIndex, "Each"),out result))?result:DBNull.Value;
      

      【讨论】:

      • 嗨 Sudhakar,我试过了,但在上面那一行中再次出现错误,因为“最佳重载方法”无效参数
      • @SriHari : 你应该声明变量结果,现在检查我编辑的答案
      • 您的代码导致错误。您的意思是使用?result:0; 而不是?result:DBNull.Value;
      【解决方案5】:

      我正在使用带有 RowDataBound 事件的 GridView 对 ASP.net VB 进行编程,在某些行中我有来自数据库的空值:

      If e.Row.RowType = DataControlRowType.DataRow Then
         If (DataBinder.Eval(e.Row.DataItem, "MyField") IsNot DBNull.Value) Then                    
             myTotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "MyField"))
         End If    
      End If
      

      【讨论】:

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