Esther EatFries
Esther EatFries

Reputation: 47

NullReferenceException when update database from datagridview

I wanna edit my datagridview in windows form and click on the "Save" button which updates to database.

public void button1_Click(object sender, EventArgs e)
{
    string txt = textBox1.Text;

    string strOleDbConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Project.mdb";
    string strSqlStatement = string.Empty;
    strSqlStatement = "SELECT * FROM jiahe WHERE [User] = '" + txt + "'";
    OleDbConnection objConnection = new OleDbConnection(strOleDbConnectionString);
    OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSqlStatement, objConnection);
    DataSet ds = new DataSet();
    objAdapter.Fill(ds);

    DataTable dt = ds.Tables[0];
    dataGridView1.DataSource = dt.DefaultView;

    try
    {
        if (dt.Rows.Count == 1)
        {
            string strLine = string.Empty;
            string strUser = string.Empty;

            foreach (DataRow dr in dt.Rows)
            {
                string strTags = dr["Tag ID"].ToString();
                strUser = dr["User"].ToString();
                string strAge = dr["Age"].ToString();
                string strPhoneNumber = dr["Phone Number"].ToString();

                DataTable dataTable = new DataTable();

                string updateString = @"update jiahe set Age = ' " + strAge + " ' where [User] = '" + textBox1.Text + "'";

                OleDbCommand cmd1 = new OleDbCommand(updateString, objConnection);

                cmd1.Connection.Open();

                string str = cmd1.ExecuteNonQuery().ToString();
                cmd1.Connection.Close();

            }
        }
        else
        {
            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("Invalid input!");
            }
        }
    }
    catch (Exception)
    {
        MessageBox.Show("Error!");
    }

}

My "Save" button:

private void button2_Click(object sender, EventArgs e)
{
    try
    {
        objAdapter.Update(dt);
    }

    catch (Exception exx)
    {
        MessageBox.Show(exx.ToString());
    }
}

When I clicked Save, I have the "Object reference not set.." error at objAdapter.Update(dt);. Please tell me what am I missing here. I'm self-learning c# and still very new, so don't be harsh on me.

Upvotes: 1

Views: 593

Answers (1)

Matt Greer
Matt Greer

Reputation: 62037

You have defined objAdapter locally in your first button handler. So the second button handler doesn't even know it exists. The code you posted shouldn't compile. Is this really your code or an approximation of it?

You will probably need to make objAdapter a member of your class. Pull it's declaration out of the methods out into the class. That should get you at least a step or two closer.

It's also possible you do have a class level objAdapter and accidentally made a second -- but local -- one inside the first handler. If that is the case, change this line

OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSqlStatement, objConnection);

to this:

objAdapter = new OleDbDataAdapter(strSqlStatement, objConnection);

Upvotes: 1

Related Questions