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.

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.

decimal? unitPrice = ...

A nullable type is a value type that accepts null as a value. 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.

But I assume that null should not be received, it would make the calculation impossible. 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.

The Problem is beacuse , the data values that are being fetched from may contain DBNull values which means that value does not exists.

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.

The synatax is:

datatype? variablename

Hopw this helps..

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

Syntax: decimal.TryParse(value,out parameter)

the above function return true if the value can be castable to decimal return false when casting is not possible

when you need to insert Null into table column you should insert DBNull.Value from the code. so you can send DBNull.Value when the casting is not possible.

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;

I am programming ASP.net VB using GridView with RowDataBound event, in some rows I have nulls from database:

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

Need Your Help

User input in python

python python-2.7

I am working on a script which provides the user a lot of options to choose from, e.g

JDBC ODBC and 4D Database : when reading a row, the first space character of a column is not sent

java jdbc odbc jdbc-odbc 4d-database

I use a remote 4D v11 database and an ODBC 4D Driver on Windows to access and read the data.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.