简体   繁体   中英

Updating database table from DataGridView in C#

I am trying to update my database rows from the DataGridView using this code:

private void button2_Click(object sender, EventArgs e)
 {
     foreach (GridViewRow dr in dataGridView1.Rows)
     {
         string constring = "Data Source = localhost; port = 3306; username = root; password = 0159";
         string Query = "Update TopShineDB.Table1 set Time = '" + dr.Cells[0].Text + "', CarColorNumber = '" + dr.Cells[1].Text + "', Interior = '" + dr.Cells[2].Text + "', Exterior = '" + dr.Cells[3].Text + "', CPlastic = '" + dr.Cells[4].Text + "', MPlastic = '" + dr.Cells[5].Text + "', SPlastic = '" + dr.Cells[6].Text + "', PlasticB = '" + dr.Cells[7].Text + "', WashExt = '" + dr.Cells[8].Text + "', WashEng = '" + dr.Cells[9].Text + "', WashTrunk = '" + dr.Cells[10].Text + "', WashSeats = '" + dr.Cells[11].Text + "', SeatsRmv = '" + dr.Cells[12].Text + "', SeatsFit = '" + dr.Cells[13].Text + "', Notes = '" + dr.Cells[14].Text + "', where Time = '" + dr.Cells[0].Text + "' ;";  
         MySqlConnection conn = new MySqlConnection(constring);
         MySqlCommand command = new MySqlCommand(Query, conn);
         MySqlDataReader myReader;

         try
         {
             conn.Open();
             myReader = command.ExecuteReader();
             MessageBox.Show("Table Successfully Updated");
             while (myReader.Read())
             {

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

But I ended up getting this error:

{"Unable to cast object of type 'System.Windows.Forms.DataGridViewRow' to type 'System.Web.UI.WebControls.GridViewRow'."}

You are doing DataReader and trying to update. To update you need to executeNonQuery. So instead of:

myReader = command.ExecuteReader();

You need

cmd.ExecuteNonQuery();

Here is how your code under foreach loop need to looks like

string constring = "Data Source = localhost; port = 3306; username = root; password = 0159";
string Query = "Update TopShineDB.Table1 set Time = '" + dr.Cells[0].Text + "', CarColorNumber = '" + dr.Cells[1].Text + "', Interior = '" + dr.Cells[2].Text + "', Exterior = '" + dr.Cells[3].Text + "', CPlastic = '" + dr.Cells[4].Text + "', MPlastic = '" + dr.Cells[5].Text + "', SPlastic = '" + dr.Cells[6].Text + "', PlasticB = '" + dr.Cells[7].Text + "', WashExt = '" + dr.Cells[8].Text + "', WashEng = '" + dr.Cells[9].Text + "', WashTrunk = '" + dr.Cells[10].Text + "', WashSeats = '" + dr.Cells[11].Text + "', SeatsRmv = '" + dr.Cells[12].Text + "', SeatsFit = '" + dr.Cells[13].Text + "', Notes = '" + dr.Cells[14].Text + "', where Time = '" + dr.Cells[0].Text + "' ;";
MySqlConnection conn = new MySqlConnection(constring);
MySqlCommand command = new MySqlCommand(Query, conn);
                                    try
                        {
                            con.Open();
                            command.ExecuteNonQuery();
                            con.Close();
                        }

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

It's foreach(DataGridViewRow... not foreach(GridViewRow... As in msdn documentation .Rows property :

Gets an array of DataGridViewRow objects.

In order for code to compile with this change, you will need to use .Value instead of .Text when you access cells values

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