when i multiply two columns in gridview in c# the error is Object cannot be cast from DBNull to other types.
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Jul 30, 2013, 10:13 AM EDT
decimal sum = 0.00m;
decimal s, s1;
for (int i = 0; i < tbl_main_subDataGridView.Rows.Count; i++)
{
if (tbl_main_subDataGridView.CurrentRow.Cells[4].Value != DBNull.Value)
s = Convert.ToDecimal(tbl_main_subDataGridView.CurrentRow.Cells[4].Value);
else
s = 0.00m;
if (tbl_main_subDataGridView.CurrentRow.Cells[5].Value != DBNull.Value)
s1 = Convert.ToDecimal(tbl_main_subDataGridView.CurrentRow.Cells[5].Value);
else
s1 = 0.00m;
decimal s13 = s1 * s;
tbl_main_subDataGridView.CurrentRow.Cells[6].Value = s13;
}
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 30, 2013, 8:21 AM EDT
You need to check null before multiply. Please share your code.
Regards,
Iftikar
hoshiar ali22 reputationPosted Jul 30, 2013, 8:52 AM EDT
decimal sum = 0.00m;
decimal s = Convert.ToInt32(tbl_main_subDataGridView.CurrentRow.Cells[4].Value);
decimal s1 = Convert.ToInt32(tbl_main_subDataGridView.CurrentRow.Cells[5].Value);
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 30, 2013, 9:25 AM EDT
Regards,
Iftikar
hoshiar ali22 reputationPosted Jul 30, 2013, 10:46 AM EDT
Regards
Hoshiar Ali