在更新表期间获得异常

本文关键字:异常 更新 | 更新日期: 2023-09-27 17:50:11

我正在更新数据库中的表,但它不断给我一个异常: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);
}

在更新表期间获得异常

我猜你正试图在你的datagridview底部的空行执行更新语句…把这个添加到你的循环中:

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