I have a project that has two windows forms on it. The first has a datagridview on it that shows the data from a database. The second form opens up from the first and is a data entry form for the data shown in the first form. I have written a method in the first form that loads the data into the datagridview (see below):
public void loadData()
{
//Method for loading data into grid
OleDbConnection dbConnectionString;
dbConnectionString = new OleDbConnection(vDbString);
dbConnectionString.Open();
string sqlString = "SELECT * FROM Problems";
var sqlDataAdapt = new OleDbDataAdapter(sqlString, dbConnectionString);
using (DataTable dt = new DataTable())
{
sqlDataAdapt.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
}
On the second form i have a button that inserts the data in the database and then closes form 2. On that button i also want it to refresh the data in the datagridview on form 1. So i am trying to call the loadData() method. However because its on another form it doesnt seem to work for me. Can anyone tell me how i get this to work?:
OleDbConnection dbConnectionString;
dbConnectionString = new OleDbConnection(Main.vDbString);
string sql = "INSERT INTO Problems (ProbID,ProbDate,ProbEmp,ProbEmp1,ProbDept,ProbOrderNo,ProbCustomer,ProbSite,ProbDetails) VALUES (@ProbID,@ProbDate,@ProbEmp,@ProbEmp1,@ProbDept,@ProbOrderNo,@ProbCustomer,@ProbSite,@ProbDetails)";
OleDbCommand command = new OleDbCommand(sql, dbConnectionString);
using (command)
{
command.Parameters.Add("@ProbID", OleDbType.VarChar).Value = newProblemID();
command.Parameters.Add("@ProbDate", OleDbType.VarChar).Value = dateTimePicker1.Text;
command.Parameters.Add("@ProbEmp", OleDbType.VarChar).Value = label10.Text;
command.Parameters.Add("@ProbEmp1", OleDbType.VarChar).Value = cbEmployee.Text;
command.Parameters.Add("@ProbDept", OleDbType.VarChar).Value = label8.Text;
command.Parameters.Add("@ProbOrderNo", OleDbType.VarChar).Value = txtOrderNo.Text;
command.Parameters.Add("@ProbCustomer", OleDbType.VarChar).Value = txtCustomer.Text;
command.Parameters.Add("@ProbSite", OleDbType.VarChar).Value = txtSite.Text;
command.Parameters.Add("@ProbDetails", OleDbType.VarChar).Value = rtbProblem.Text;
dbConnectionString.Open();
command.ExecuteNonQuery();
MessageBox.Show("Problem Added to Problem Book Successfully");
Main.loadData();
this.Close();
dbConnectionString.Close();
}
The Main.loadData() line on the above is showing the following error: An object reference is required for the non-static field, method, or property 'Main.loadData()'