Click here to Skip to main content
14,268,184 members
Rate this:
Please Sign up or sign in to vote.
Hi, im working on a datagridview and update rows using checkbox, and program works, but i have problems in Messagebox showing multiple Message "Success"

Here's my code Displaying on datagridview

MySqlDataAdapter sda = new MySqlDataAdapter("Select * from Foreclosed", con);
  DataTable dt = new System.Data.
 sda.Fill(dt);
metroGrid1.Rows.Clear();
foreach (DataRow item in dt.Rows)
    {
      int n = metroGrid1.Rows.Add();
       metroGrid1.Rows[n].Cells[0].Value = "false";
       metroGrid1.Rows[n].Cells[1].Value = item["Bank_Code"].ToString();
       metroGrid1.Rows[n].Cells[2].Value = item["Property_Code"].ToString();
       metroGrid1.Rows[n].Cells[3].Value = item["Region"].ToString();
       metroGrid1.Rows[n].Cells[4].Value = item["Province"].ToString();
       metroGrid1.Rows[n].Cells[5].Value = item["Detailed_Address"].ToString();


For the Update Button

foreach (DataGridViewRow item in metroGrid1.Rows)
{
    var cell = item.Cells[0];
    var value = cell.Value;
    if (cell != null)
    {
        if (value != null && (bool)value == true)
        { 
            con.Open();
            MySqlCommand com2 = new MySqlCommand("UPDATE Foreclosed SET Bank_Code=@bankcode WHERE Property_Code='" + item.Cells[2].Value.ToString() + "'", con);
            com2.Parameters.AddWithValue("@bankcode", fmupdtxtBox.Text);
            com2.ExecuteNonQuery();
            con.Close();
        }
        MessageBox.Show("Success");
    }
}


Kindly help me how to avoid getting a loop message
Posted
Updated 1-Dec-15 16:15pm
v8

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

If you don't want a message to be shown for every iteration, just move the line
MessageBox.Show("Success");

outside the foreach loop.

Put a boolean inside the loop that you set to true if the transaction is successful and then outside the loop you add an if-statement.
if (succcess)
    MessageBox.Show("Success");
   
Comments
Rencyrence 1-Dec-15 23:52pm
   
Hello, it works, but i tried to play with checkbox, i tried to remove the check from checkbox and try to click Update Button, but the message was shown.
Rencyrence 2-Dec-15 0:03am
   
Hello again, i already fix it, by using try finally, and set to default success=false; im wondering now how will i catch if there's no more selected checkbox, and display a message, tell me if i need to create another thread, thanks
Rencyrence 2-Dec-15 0:42am
   
I got it already , thanks
George Jonsson 2-Dec-15 0:44am
   
Sometimes it helps to do it yourself :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100