-3

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()'

Peter
  • 29
  • 3
  • 2
    [Duplicate 1](http://stackoverflow.com/questions/3062575/), [duplicate 2](http://stackoverflow.com/questions/7800731/), [duplicate 3](http://stackoverflow.com/questions/17032484/), [duplicate 4](http://stackoverflow.com/questions/17836398/), [duplicate 5](http://stackoverflow.com/questions/25316230/), [duplicate 6](http://stackoverflow.com/questions/29092707/) ... – Dour High Arch Oct 22 '19 at 15:58
  • 1
    Calling a method from another form is not a good practice- It can be done, but not advisable. It would be more appropriate for the second form to raise an event. When the first form initializes the second one, attach an event handler the first one. Then the second form raises the event, the first one catches it, and reloads the data. As a side note, from the sounds of it, you're just starting down the programming path. I suggest you research and write your application in WPF. The data binding features built in and the MVVM pattern are more modern approaches to this issue. – Andy Stagg Oct 22 '19 at 15:59
  • 3
    It would be better to just pass the `dt` to form2, bind the text boxes to it, let the user edit it and close it, then use another data adapter to sent the changes to the datatable back to the db. You've written a lot of code that you can get visual studio to write for you with a few clicks - take a look at some data binding tutorials like https://msdn.microsoft.com/en-us/ie/aa984466(v=vs.94) (it's getting pretty old now, but short of switching to dapper or EF there isn't much that has changed if you want to use datasets ) – Caius Jard Oct 22 '19 at 16:08

1 Answers1

0

You can make MainForm as a singleton to be able to call the method.

public class MainForm : Form
{
  static internal readonly MainForm Instance = new MainForm();
}

Or any singleton implementation you like.

And you will change the Program.Main:

Application.Run(MainForm.Instance);

Now you can call:

MainForm.Instance.loadData();