运行代码时出现“ System.NullReferenceException:'对象引用未设置为对象的实例。'”错误

时间:2019-01-30 15:27:42

标签: c# winforms

我对编码还很陌生,所以我可能很容易错过这一点。当我运行我的代码时,我得到一个System.NullReferenceException:'对象引用未设置为对象的实例。当我按下表单上的提交按钮时。应该将文本框信息添加到表中,然后更新dataGridView。

SELECT s.spelersnr, s.naam,
    ROW_NUMBER() OVER (ORDER BY b.mbedrag DESC, s.spelersnr DESC) rn
FROM spelers s
INNER JOIN
(
    SELECT spelersnr, MAX(bedrag) AS mbedrag
    FROM boetes
    GROUP BY spelersnr
) b
    ON s.spelersnr = b.spelersnr
ORDER BY
    b.mbedrag;

此行不断给我错误:

string strConnect = @"Provider=Microsoft.ACE.OLEDB.12.0;Data 
Source='C:\\temp\\ScrapperDatabase.accdb'";
        OleDbConnection conn = new OleDbConnection(strConnect);

        string addToTable = "UPDATE ScrapperTable (Shift, Machine, Weight, Operator, Date/Time)" +
            "VALUES ('" + textBox2.Text + "','" + comboBox2.Text + "','" + textBox1.Text + "','" + textBox3 + "','" + DateTime.Now + "')";

        DataSet ds1 = new DataSet("ScrapperTable");
        OleDbConnection cn = new OleDbConnection(strConnect);
        cn.Open();
        string sql = "SELECT * FROM ScrapperTable";
        System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter(sql, cn);
        da.Fill(ds1);

        DataTable dt = ds1.Tables[0];
        DataRow dRow = dt.NewRow();
        //Testing lines
        dRow[1] = textBox2.Text;
        dRow[2] = textBox1.Text;
        dRow[3] = textBox1.Text;
        dRow[4] = textBox1.Text;
        dRow[5] = DateTime.Now;

        ds1.Tables["ScrapperTable"].Rows.Add(dRow);

        da.Update(ds1, "ScrapperTable");

        cn.Close();

0 个答案:

没有答案