hi everyone, i'm new here also new in c#.Please help me to re-populate my listview in form1 from the database when i close form2(modal). is is from form2 formClosing event

private void Client_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = false;
            ClientInfo a = new ClientInfo();
            a.displayClient();//displayClient() is responsible for displaying   //items from database
                              //also i called it during form1_Load()
            a.Show();
        }

Is there something wrong with my displayClient()? here it is from form1

public void displayClient()
        { 
            dbClient db = new dbClient();
            string con = db.Mycon();
            MySqlConnection cnMySQL = new MySqlConnection(con);
            cnMySQL.Open();
            MySqlCommand cmdMySQL = cnMySQL.CreateCommand();
            MySqlDataReader reader;
            cmdMySQL.CommandText = "select * from CLIENT";
            reader = cmdMySQL.ExecuteReader();
            lvClient.Items.Clear();
            MessageBox.Show("hi");
            while (reader.Read())
            {
                int i = reader.FieldCount;
                ListViewItem item1 = new ListViewItem(reader.GetString(0), i);
                item1.SubItems.Add(reader.GetString(1));
                item1.SubItems.Add(reader.GetString(2));
                item1.SubItems.Add(reader.GetString(3));
                item1.SubItems.Add(reader.GetString(4));
                //Add the items to the ListView.
                lvClient.Items.AddRange(new ListViewItem[] { item1 });
                // Add the ListView to the control collection.
                this.Controls.Add(lvClient);
                i++;
            }
            cnMySQL.Close();
        }

Please help.Thanks

You are creating a new instance of ClientInfo and calling the displayClient method on that instance. Is ClientInfo the name of your main form?
You need to run the displayClient method from the original form, not in a new instance.
The best way to do that (if you are showing the second form modally) is to call the method after showing the second form:

Form2 frm2 = new Form2();
frm2.ShowDialog();
displayClient();

You could also set a dialogresult in Form2's closing method and check for that in Form1:

private void Client_FormClosing(object sender, FormClosingEventArgs e)        {            e.Cancel = false;            ClientInfo a = new ClientInfo();            a.displayClient();//displayClient() is responsible for displaying   //items from database                              //also i called it during form1_Load()            a.Show();        }private void Client_FormClosing(object sender, FormClosingEventArgs e)
        {
            //check that required values have been set
            this.DialogResult = DialogResult.OK;
        }

//then in form1
Form2 frm2 = new Form2();
DialogResult result = frm2.ShowDialog();
if(result == DialogResult.OK)
    displayClient();
commented: Thank you sir,it works! +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.