简体   繁体   中英

Insert DataGridView values into MySql database. c#

I want to add new values to MySql database from dataGridView1. The code itself appears to be correct, no errors in Visual Studio 2012, but there is no data inserted in my db. Here's the code I'm using:

private void button2_Click(object sender, EventArgs e)
{
   confirm exec = new confirm();      
}

public class confirm
{
   public void method(DataGridViewCellEventArgs f)
   {
      DataGridView dataGridView1 = new DataGridView();
      Label label1 = new Label(); // contains User ID which is used for payer_code
      Label label6 = new Label(); // contains current dayTime

      foreach (DataGridViewRow row in dataGridView1.Rows)
      {
         if ((bool)dataGridView1.Rows[f.RowIndex].Cells["paidDataGridViewTextBoxColumn"].Value == true)
         {
            try
            {
               string MyConnectionString = "Server=localhost; Database=contractsdb; Uid=root; Pwd=";
               MySqlConnection connection = new MySqlConnection(MyConnectionString);
               MySqlCommand cmd = new MySqlCommand();
               cmd = connection.CreateCommand();
               connection.Open();
               cmd.CommandText = "INSERT INTO payments(pay_name, pay_code, payer_code, pay_sum, pay_date)VALUES(@pay_name, @pay_code, @payer_code, @pay_sum, @pay_date)";
               cmd.Parameters.AddWithValue("@pay_name", dataGridView1.Rows[f.RowIndex].Cells["contractnameDataGridViewTextBoxColumn"].Value);
               cmd.Parameters.AddWithValue("@pay_code", dataGridView1.Rows[f.RowIndex].Cells["contractcodeDataGridViewTextBoxColumn"].Value);
               cmd.Parameters.AddWithValue("@payer_code", label1.Text);
               cmd.Parameters.AddWithValue("@pay_sum", dataGridView1.Rows[f.RowIndex].Cells["sumDataGridViewTextBoxColumn"].Value);
               cmd.Parameters.AddWithValue("@pay_date", label6.Text);
               cmd.ExecuteNonQuery();
               connection.Close();
            }   

            catch (Exception ex)
            {
               MessageBox.Show(ex.Message);
            }
         }
      }
   }
}

I think you are misunderstanding something about OOP. Do it like this:

your confirm class method should also have the reference of datagridview1 (you are creating an empty datagridview so it never goes even into the foreach loop)

public void method(DataGridView datagridview1) //remove your first argument, you don't need it anymore
{
    //delete the line "DataGridView dataGridView1 = new DataGridView();"
    //and keep the rest of the code as it is
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        if(row.Cells["paidDataGridViewTextBoxColumn"].Value == true) //it will check every row, and you don't need "DataGridViewCellEventArgs" argument now
        {
            try
            {
                //your code, it will be same here
            }
        }
}

for calling the method:

(use the same button_click event as you were doing)

private void button2_Click(object sender, EventArgs e)
{
    confirm exec = new confirm();      
    exec.method(datagridview1); //pass "datagridview1" reference
}

It will pass the reference of your original datagridview1 to the confirm class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM