简体   繁体   English

无法将对象从DBNull强制转换为显示的其他类型错误?

[英]Object cannot be cast from DBNull to other types error shown?

My code is this 我的代码是这样

    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);
    }

Here I assume, in case of Null it set the unitPrice to 0. 在这里,我假设在Null的情况下将unitPrice设置为0。

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

If there can be null value try using nullable types. 如果可以有null值,请尝试使用可为null的类型。

decimal? unitPrice = ...

A nullable type is a value type that accepts null as a value. 可为空的类型是一种将null作为值的值类型。 You can then check value 然后可以检查值

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

More about nullables on MSDN . 有关MSDN上的 nullables的更多信息。

But I assume that null should not be received, it would make the calculation impossible. 但是我假设不应接收null,这将使计算变得不可能。 Therefore I would advise to encapsulate fetching values in a try/catch block instead and return from method if some of the calls throws an exception. 因此,我建议将获取值封装在try/catch块中,如果某些调用引发异常,则从方法中返回。

you can use TryParse method to identify whether cast is possible from given value to decimal or not.if not possible assign the DBNull.Value 您可以使用TryParse方法来确定是否可以从给定值转换为十进制。如果不可能,则分配DBNull.Value

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

the above function return true if the value can be castable to decimal 如果该值可以强制转换为decimal则上述函数返回true
return false when casting is not possible 如果无法投放,则返回false

when you need to insert Null into table column you should insert DBNull.Value from the code. 当需要在表列中插入Null ,应从代码中插入DBNull.Value so you can send DBNull.Value when the casting is not possible. 因此您可以在无法进行强制转换时发送DBNull.Value

Note: here i have used ternary ?: operator to write the whole thing in single line 注意:这里我使用三元?:运算符将整个事情写在单行中
Solution: 解:

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

The Problem is beacuse , the data values that are being fetched from may contain DBNull values which means that value does not exists. 问题是因为,从中获取的数据值可能包含DBNull值,这意味着该值不存在。

So, it makes not sense in changing the type of the value that has not existed. 因此,更改不存在的值的类型没有意义。

The solution to allow null is , declare the variable as nullable types that means they are capable of accepting null values. 允许null的解决方案是,将变量声明为可为null的类型,这意味着它们能够接受null值。

The synatax is: 语法是:

datatype? variablename

Hopw this helps.. 霍普这有帮助。

I am programming ASP.net VB using GridView with RowDataBound event, in some rows I have nulls from database: 我正在使用GridView和RowDataBound事件对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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM