Click here to Skip to main content
16,016,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have bankname combobox,accountno textbox and Accounttype combobox.If i select bankname from the combobox.It shows the related data in datagridview from database table.If select
"Select " text from the bankcombobox.It doesn't show any data in datagridview.I want to add new row top of the datagridview when i click addbutton.new row is adding succesfully.In datagridview i have textboxes like slno,startno,noofcheques and endingno and status.after entering startno and noofcheques both will add populate data in ending no text for that i am using cell leave event.when i load the form firsttime new row is adding and values are calculating fine.when i first select bankname and then select "select" text click addnew row button row is adding but when i try to enter values into the startno textbox.It throws error.'System.InvalidCastException' occurred in mscorlib.dll.Object cannot be cast from DBNull to other types.
I'm fresher please help me on this.

What I have tried:

C#
       private void dgvwChqs_CellLeave(object sender, DataGridViewCellEventArgs e)
       {

          if (dgvwChqs.Rows[e.RowIndex].Cells["startno"].Value != null)
           {
               dgvwChqs.Rows[e.RowIndex].Cells["endingno"].Value =
           Convert.ToInt32(dgvwChqs.Rows[e.RowIndex].Cells["startno"].Value) + Convert.ToInt32(dgvwChqs.Rows[e.RowIndex].Cells["noofcheques"].Value);
           }
       }


private void btnadd_Click(object sender, EventArgs e)
       {

           dts.Rows.InsertAt(dts.NewRow(), 0);



           }
Posted
Updated 14-May-17 20:26pm

1 solution

An error message: System.InvalidCastException' occurred in mscorlib.dll.Object cannot be cast from DBNull to other types. is quite clear. It means that one of startno and noofcheques field is null!
You can check if the value is not null by using IsDbNull[^] method or you can replace null with default value:

C#
//first method
int startno = 0;
int noofcheques = 0;
if(!Convert.IsDbNull(dgvwChqs.Rows[e.RowIndex].Cells["startno"].Value))
    startno = dgvwChqs.Rows[e.RowIndex].Cells["startno"].Value; //assuming that startno field stores integer values
if(!Convert.IsDbNull(dgvwChqs.Rows[e.RowIndex].Cells["noofcheques"].Value))
    noofcheques = dgvwChqs.Rows[e.RowIndex].Cells["noofcheques"].Value;

//second method
int startno = Convert.IsDbNull(dgvwChqs.Rows[e.RowIndex].Cells["startno"].Value)==true ? dgvwChqs.Rows[e.RowIndex].Cells["startno"].Value : 0;
int noofcheques = Convert.IsDbNull(dgvwChqs.Rows[e.RowIndex].Cells["noofcheques"].Value)==true ? dgvwChqs.Rows[e.RowIndex].Cells["noofcheques"].Value : 0;

//finally:
dgvwChqs.Rows[e.RowIndex].Cells["endingno"].Value = startno + noofcheques;


Another way is to use nullable datatypes. For further details, please see: Using Nullable Types (C# Programming Guide) | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
Member 13153537 15-May-17 2:35am    
Thanks you for your response.
Maciej Los 15-May-17 2:36am    
You're very welcome.
Cheers,
Maciej

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