c# - Object cannot be cast from DBNull to other types in Grid view Controll -


i trying achieve code printing grand total in gridview footer using rowdatabound method. declared 2 variables inside rowdatabound method . 1 money in , money out.gridview has empty rows.i have 2 table in database 1 call deposit , call withdraw. merging 2 table using sqldataadapter.i declared 2 data table dt , dt1.inside sqldataadapter of each data table passed 2 sql query retrieve data database using 1 key coming textbox. merging 2 data table using merge method , displaying in grid view. works when trying grand total of columns having error object cannot cast dbnull other types. here sql code.

     sqlconnection cn = new sqlconnection(@"data source=khundokarnirjor\khundokernirjor;initial catalog=login;integrated security=true");          sqldataadapter sdr = new sqldataadapter(@"select  account_number [account number],amount as[money in]   deposit  deposit.account_number='" + textbox1.text + "'", cn);         datatable dt = new datatable();         sdr.fill(dt);         sqldataadapter sdr1 = new sqldataadapter(@"select    withdraw.amount as[money out], account.statementamount [balance] account                                              inner join withdraw                                              on account.account_number = withdraw.account_number                                               withdraw.account_number='" + textbox1.text + "'", cn);         datatable dt1 = new datatable();         sdr1.fill(dt1);         dt.merge(dt1);         gridview2.datasource = dt;         gridview2.databind();     }      int moneyin=0;         int moneyout=0;           // loop thru each data row , compute total unit price , quantity sold          if (e.row.rowtype == datacontrolrowtype.datarow)         {             if (databinder.eval(e.row.dataitem, "money in") != dbnull.value)             {                 moneyin += convert.toint32(databinder.eval(e.row.dataitem, "money in"));             }             if (databinder.eval(e.row.dataitem, "money out") != dbnull.value)             {                 moneyout += convert.toint32(databinder.eval(e.row.dataitem, "money out"));             }         } //// display totals in gridview footer         else if (e.row.rowtype == datacontrolrowtype.footer)         {             e.row.cells[0].text = "grand total";             e.row.cells[0].font.bold = true;              e.row.cells[1].text = moneyin.tostring();             e.row.cells[1].font.bold = true;              e.row.cells[2].text = moneyout.tostring();             e.row.cells[2].font.bold = true;         }     } }  please can me solve problem.   here out put after editing , cheeking db null referance still can not add grand total  

have tried comparing dbnull before converting?

if(databinder.eval(e.row.dataitem, "money in") != dbnull.value) {   moneyin += convert.toint32(databinder.eval(e.row.dataitem, "money in")); } 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -