Hello I'm having a problem here. The code looks like this.
private void Form3_Load(object sender, EventArgs e)
{
string connectionString =
"Server=localhost;" +
"Database=oroderm;" +
"User ID=root;" +
"Password=root;" +
"Pooling=false";
string query = "Select * from client";
MySqlConnection conn = new MySqlConnection(connectionString);
MySqlDataAdapter dAdapter = new MySqlDataAdapter(query, connectionString);
conn.Open();
DataSet ds = new DataSet();
MySqlCommandBuilder cBuilder = new MySqlCommandBuilder(dAdapter);
dAdapter.Fill(ds, "client");
BindingSource bSource = new BindingSource();
bSource.DataSource = ds.Tables["client"];
dataGridView2.DataSource = bSource;
}
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = ds.Tables["client"];
this.dataGridView2.BindingContext[dt].EndCurrentEdit();
this.da.Update(dt);
}
So what I want is that whenever I edit the values in my Datagrid it will affect my database after I click button1 (save button). ex. If I have roman as the client after I changed his name and I click button1 it should change. However Im getting a value cannot be null error. Please can somebody help. T_T
*EDIT: Heres the Updated Code
private MySqlDataAdapter _da;
private DataTable _dt;
private DataSet _ds;
private void Form3_Load(object sender, EventArgs e)
{
updateClient();
}
public void updateClient()
{
string connectionString =
"Server=localhost;" +
"Database=oroderm;" +
"User ID=root;" +
"Password=root;" +
"Pooling=false";
string query = "select * from client";
MySqlConnection conn = new MySqlConnection(connectionString);
_da = new MySqlDataAdapter(query, connectionString);
conn.Open();
_ds = new DataSet();
MySqlCommandBuilder cBuilder = new MySqlCommandBuilder(_da);
_da.Fill(_ds, "client");
BindingSource bSource = new BindingSource();
bSource.DataSource = _ds.Tables["client"];
dataGridView2.DataSource = bSource;
_da.UpdateCommand = cBuilder.GetUpdateCommand();
}
private void button1_Click(object sender, EventArgs e)
{
_dt = _ds.Tables["client"];
this.dataGridView2.BindingContext[_dt].EndCurrentEdit();
this._da.Update(_dt);
}
I assume you've got an instance variable called ds
, and you're expecting the Form3_Load
method to populate it? That won't happen - because in Form3_Load
you've got this:
DataSet ds = new DataSet();
That's declaring a new local variable called ds
, so unless you explicitly use this.ds
in the method, you're not going to change the value of the instance
variable.
I'm slightly surprised that you're not getting a NullReferenceException
when you click on the button, but maybe you've got some code to create an empty DataSet
and assign a reference to the ds
instance variable somewhere. Anyway, changing the line shown above to just:
ds = new DataSet();
should fix this problem.
(I would strongly advise against making database requests in the UI thread, mind you, and you should have a using
statement for your connection, but those are separate matters.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With