Good day everyone,
Firstly I would like to thank anyone in advance who takes the time to look through this lengthy post.
Context: I am building a simple GUI interface using windows forms and c sharp that connects to a MySql database to perform CRUD operations.
Problem: I am trying to implement a method that would take the necessary MySql code elements as arguments and pull a table into a data grid view. I have produced 2 methods - 1 that takes arguments and the other one which is hard coded to pull the data.
After pulling the data I am able to insert/amend any records in the database with a SAVE button method. The issue is that when I use the method that takes the arguments to pull the data into the grid view I am not able to SAVE my changes as I get a null
reference error. However when using the hard coded method I don't get any null
errors and all works fine. For some reason when the sqlData
and dataTable
variables are passed to the SAVE button method AFTER the grid view gets filled with the method based on arguments they end up as null
. Any experts that have experience building these applications have any advice please?
Hard coded method for pulling data into grid view:
private void fill_grid_view1(string sequelQueryString)
{
using (MySqlConnection mysqlConn = new MySqlConnection(db_connection))
{
mysqlConn.Open();
sqlData = new MySqlDataAdapter(sequelQueryString, mysqlConn);
sqlCommandBuilder = new MySqlCommandBuilder(sqlData);
dataTable = new DataTable();
sqlData.Fill(dataTable);
dataGridView1.DataSource = dataTable;
}
}
The SAVE button methods:
private void bttnSave_Click(object sender, EventArgs e)
{
save_changes_refresh(sqlData, dataTable);
}
private void save_changes_refresh(MySqlDataAdapter given_data_adapter, DataTable given_data_table)
{
try
{
given_data_adapter.Update(given_data_table);
select_both_user_tweet();
MessageBox.Show("Your changes have been saved successfully!"); //
}
catch (Exception errobj)
{
MessageBox.Show(errobj.Message.ToString());
}
}
The method I want to use to pull data into grid view based on given arguments:
private void fill_given_grid_view (string sequelQueryString, MySqlDataAdapter given_data_adapter, DataTable given_data_table, DataGridView given_grid_view,
MySqlCommandBuilder given_builder)
{
using (MySqlConnection mysqlConn = new MySqlConnection(db_connection))
{
mysqlConn.Open();
given_data_adapter = new MySqlDataAdapter(sequelQueryString, mysqlConn);
given_builder = new MySqlCommandBuilder(given_data_adapter);
given_data_table = new DataTable();
given_data_adapter.Fill(given_data_table);
given_grid_view.DataSource = given_data_table;
}
}
All the new method does is pull data based on arguments so that if I had let's say 5 dataGridView elements I wouldn't need to hard code all five pull methods separately like I did in the first code snippet. And it works but it doesn't let me save any changes as mentioned above because of the sqlData
and dataTable
variables end up as null when I try to execute the save method.
Method that passes the needed parameters to fill_given_grid_view
:
private void view_users_Click(object sender, EventArgs e)
{
fill_given_grid_view("SELECT * FROM new_schema.user", sqlData, dataTable, dataGridView1, sqlCommandBuilder);
}
EDIT: I've read the possible duplicate thread and it is useful however I struggle to understand why essentially using 2 methods that do the same thing one of them drops the sqlData
and dataTable
variables to null and the hard coded method from the first snippet does not drop the variables and keeps the needed values to pass into the SAVE method.