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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • possible duplicate of [What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – John Saunders Dec 12 '11 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. – John Saunders Dec 12 '11 at 19:03

1 Answers1

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);
Matt Greer
  • 60,826
  • 17
  • 123
  • 123
  • That was clear, thanks. After i changed this line, it says my Value is null. What does it really mean by null? – Esther EatFries Dec 12 '11 at 19:10
  • 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? – Matt Greer Dec 12 '11 at 19:40