Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
My code is this i dono what's my error, Please help me.
C#
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);
    }


Thanks in advance.
Posted
Updated 26-Sep-16 1:20am

The error is pretty explicit: "Object cannot be cast from DBNull to other types"
Your database field can (and does) contain SQL NULL values - and when they are returned via a query, the value is a specific code which represents this in a way that your code can check and use: DBNull.Value
There are good reasons why this is not just null - mainly to do with being able to tell the difference between a missing parameter and a null DB value - but if your DB table can contain nulls, then you need to check the returned value to ensure it is not DBNull.Value before you attempt to cast it, as nulls can't be turned into valid objects.
 
Share this answer
 
Comments
Bal krishna sharma 1-Nov-22 8:02am    
We have to pass table Values name is NOT NULL write
OriginalGriff 1-Nov-22 8:35am    
Do you want to try that again, but using English sentences this time?
Try this....


C#
string value =  view.GetListSourceRowCellValue(listSourceRowIndex, "Each") +"" ;
           decimal unitPrice;
           decimal.TryParse(value, out unitPrice);
 
Share this answer
 
you copyed the code from there, right?

http://documentation.devexpress.com/#WindowsForms/CustomDocument1477[^]

but please check if you may add something like ".Value" or ".ToString" behind the request.
Like following:
C#
decimal unitPrice = Convert.ToDecimal(view.GetListSourceRowCellValue(listSourceRowIndex, "Each").Value);


i have no possebilitys tp create a project which contains a GridView right now.
so i just have to guess.
 
Share this answer
 
v2
C#
I'm thinking that your output parameter is coming back with a DBNull value. Add a check for that like this

C#
var outputParam = dataAccCom.GetParameterValue(IDbCmd, "op_Id");
if(!(outputParam is DBNull))
     DataTO.Id = Convert.ToInt64(outputParam);

 
Share this answer
 
v2
Comments
[no name] 26-Sep-16 7:27am    
Very well done. The question was asked and answered 3 years ago but those really valuable worthless internet points have you answering anyway.
check your stored procedure and

set variable as set @conducted=0

alter procedure dbo.usp_Batch

(

@batchid int,

@sturegno int,

@intresult int output,

@total int output,

@conducted int output,

@attendent int output

)

as

BEGIN

declare @lec varchar(max)

declare @sql nvarchar(max),@sql1 nvarchar(max),@sql2 nvarchar(max)

declare @out1 nvarchar(max),@out2 nvarchar(max),@out3 nvarchar(max)
set @total=0
set @conducted=0
set @attendent=0

set @intresult=1
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900