在更新表时获取异常

时间:2014-03-13 01:55:42

标签: c# datagridview

我正在更新数据库中的表,但它不断给我一个异常:NullReferenceException"对象引用未设置为对象的实例。"

我已将该片段粘贴下来:

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    double bal_due, a, b;
    a = double.Parse(dataGridView1.Rows[i].Cells[3].Value.ToString()); //this lines throws the exception
    b= double.Parse(dataGridView1.Rows[i].Cells[4].Value.ToString());
    bal_due = a - b;                        
    string var = string.Format(
        "update purchase_order set paid_today={0}, " + 
        "balance_due={1} where order_no={2}",
        dataGridView1.Rows[i].Cells[4].Value, bal_due, comboBox2.SelectedValue);
    obj.query(var);
}

1 个答案:

答案 0 :(得分:0)

我猜您正在尝试为datagridview底部的空行执行更新语句...将其添加到循环中:

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    if (dataGridView1.Rows[i].IsNewRow) continue;
    ...
相关问题