1

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.

2
  • possible duplicate of What is a NullReferenceException in .NET? Commented Dec 12, 2011 at 19:02
  • 1
    Here's a hint: I bet it's not the same objAdapter. One is a class member, the other is a local variable. Commented Dec 12, 2011 at 19:03

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

That was clear, thanks. After i changed this line, it says my Value is null. What does it really mean by null?
null means your variable is not pointing at an object. The variable itself isn't actually the object, but it refers to it. If it's null in your second handler, then your second handler is probably running before the first one? Maybe in your class's constructor you should create the objAdapter?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.