I need to read only the last record bringing this data here (lblTotalBancoCons.Text). But I receive this error: Object cannot be cast from DBNull to other types.
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dt);
conn.Close();
dvgBancoCons.DataSource = dt;
///////////////////////////////////
int sum = 0;
for (int i = 0; i < dvgBancoCons.Rows.Count; ++i)
{
sum += Convert.ToInt32(dvgBancoCons.Rows[i].Cells[10].Value);
lblTotalBancoCons.Text = " " + sum.ToString();
dvgBancoCons.CurrentCell = dvgBancoCons.Rows[dvgBancoCons.Rows.Count - 1].Cells[0];

VulpesRank #55 · 29.2K reputationPosted Feb 19, 2015, 7:06 AM EST
sum += Convert.ToInt32(dvgBancoCons.Rows[i].Cells[10].Value);
with:
object o = dvgBancoCons.Rows[i].Cells[10].Value;
if (!Convert.IsDBNull(o))
{
sum += Convert.ToInt32(o);
}
IsraelRank #1,223 · 1.5K reputationPosted Feb 19, 2015, 8:14 AM EST
It's quity perfect. But let's me disturb little bit again once.
The error it's resolved. Then I need to read only the last record pushing my cursor at the last line. But I cannot.
VulpesRank #55 · 29.2K reputationPosted Feb 19, 2015, 9:05 AM EST
So you'll probably want the row before that which will be:
dvgBancoCons.CurrentCell = dvgBancoCons.Rows[dvgBancoCons.Rows.Count - 2].Cells[0];