Note that there are some explanatory texts on larger screens.

plurals
  1. PONullReferenceException when update database from datagridview
    text
    copied!<p>I wanna edit my datagridview in windows form and click on the "Save" button which updates to database.</p> <pre><code>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!"); } } </code></pre> <p>My "Save" button:</p> <pre><code>private void button2_Click(object sender, EventArgs e) { try { objAdapter.Update(dt); } catch (Exception exx) { MessageBox.Show(exx.ToString()); } } </code></pre> <p>When I clicked Save, I have the "Object reference not set.." error at <strong>objAdapter.Update(dt);</strong>. Please tell me what am I missing here. I'm self-learning c# and still very new, so don't be harsh on me. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload