Praveen Maurya

Praveen Maurya

  • NA
  • 146
  • 27k

Object cannot be cast from DBNull to other types

Apr 26 2018 7:33 AM
i want to calculate sum table values but at startup my application the table is having null value and it show error like .....Object cannot be cast from DBNull to other types
 
Please suggest how i can solve this.
 
 
  1. SqlDataAdapter sda21 = new SqlDataAdapter("select * from sale_total", con);  
  2. DataTable dt21 = new DataTable();  
  3. sda21.Fill(dt21);  
  4. int sum = Convert.ToInt32(dt21.Compute("SUM(gtot)"string.Empty));  
  5. SqlDataAdapter sda22 = new SqlDataAdapter("select * from exp_total", con);  
  6. DataTable dt22 = new DataTable();  
  7. sda22.Fill(dt22);  
  8. int sum1 = Convert.ToInt32(dt22.Compute("SUM(gtot)"string.Empty));  
  9. metroTile13.Text = "\u20B9 " + sum1.ToString() + ".00";  
  10. if (sum1 < sum)  
  11. {  
  12. int sum2 = sum - sum1;  
  13. label2.Text = "\u20B9 " + sum2.ToString() + ".00";  
  14. metroTile6.Text = "\u20B9 " + sum2.ToString() + ".00";  
  15. }  
  16. if (sum1 > sum)  
  17. {  
  18. int sum2 = sum1 - sum;  
  19. label2.Text = "\u20B9 " + sum2.ToString() + ".00";  
  20. metroTile6.Text = "\u20B9 " + sum2.ToString() + ".00";  
  21. }  
 
 
 

Answers (6)