Ashfaque Baig

Ashfaque Baig

  • NA
  • 66
  • 2.6k

Object cannot be cast from DBNull to other types.

Apr 13 2020 10:22 AM
I am getting issue while tab reach to next new row after loading data from database.
Can anyone please correct the code
  1. private void dataGridSALEITEM_CellValueChanged(object sender, DataGridViewCellEventArgs e) {  
  2.  decimal TOTALAMOUNT = 0;  
  3.  decimal TOTALCGST = 0;  
  4.  decimal TOTALSGST = 0;  
  5.  decimal NETTOTAL = 0;  
  6.  foreach(DataGridViewRow row in dataGridSALEITEM.Rows) {  
  7.   row.Cells[dataGridSALEITEM.Columns[7].Index].Value = (Convert.ToInt32(row.Cells[dataGridSALEITEM.Columns[3].Index].Value) * Convert.ToInt32(row.Cells[dataGridSALEITEM.Columns[4].Index].Value));  
  8.   row.Cells[dataGridSALEITEM.Columns[8].Index].Value = (Convert.ToInt32(row.Cells[dataGridSALEITEM.Columns[7].Index].Value) / 200 * Convert.ToInt32(row.Cells[dataGridSALEITEM.Columns[5].Index].Value)); 
  9.   row.Cells[dataGridSALEITEM.Columns[9].Index].Value = (Convert.ToInt32(row.Cells[dataGridSALEITEM.Columns[7].Index].Value) / 200 * Convert.ToInt32(row.Cells[dataGridSALEITEM.Columns[5].Index].Value)); 
  10.   row.Cells[dataGridSALEITEM.Columns[10].Index].Value = (Convert.ToInt32(row.Cells[dataGridSALEITEM.Columns[7].Index].Value) + Convert.ToInt32(row.Cells[dataGridSALEITEM.Columns[8].Index].Value) + Convert.ToInt32(row.Cells[dataGridSALEITEM.Columns[9].Index].Value));  
  11.   TOTALAMOUNT += Convert.ToDecimal(row.Cells[dataGridSALEITEM.Columns[7].Index].Value);  
  12.   TOTALCGST += Convert.ToDecimal(row.Cells[dataGridSALEITEM.Columns[8].Index].Value);  
  13.   TOTALSGST += Convert.ToDecimal(row.Cells[dataGridSALEITEM.Columns[9].Index].Value);  
  14.   NETTOTAL += Convert.ToDecimal(row.Cells[dataGridSALEITEM.Columns[10].Index].Value);  
  15.  }  
  16.  txtSUBTOTAL.Text = TOTALAMOUNT.ToString();  
  17.  txtCGST.Text = TOTALCGST.ToString();  
  18.  txtSGST.Text = TOTALSGST.ToString();  
  19.  txtGTOTAL.Text = NETTOTAL.ToString();  
  20. }  

Answers (3)