title: Insert values from DataGridView to MySql-Database during runtime
body: <p>I got a problem with my DataGridview: I can save some value in my MySql-Database by pressing a button with the following code:</p>
  
  <p><strong>Code</strong></p>
  
  <pre><code>   private void button8_Click(object sender, EventArgs e)
      {
          string connStr = "datasource=localhost;port=3306;username=root;password=123";
          MySqlConnection conn = new MySqlConnection(connStr);
  
          try
          {
              string sql = "select *from test.edata ;";
              MySqlDataAdapter da = new MySqlDataAdapter(sql,conn);
              MySqlCommandBuilder cb = new MySqlCommandBuilder(da);
  
              MySqlCommand cmd = new MySqlCommand();
              cmd.Connection = conn;
              cmd.CommandText = sql;
  
              MySqlCommand insertCmd =cb.GetInsertCommand().Clone();
              insertCmd.CommandText = insertCmd.CommandText +";SELECT last_insert_id() AS id";
              insertCmd.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord;
              da.InsertCommand = insertCmd;
              cb.DataAdapter = null;
  
              DataTable dt = new DataTable();
              da.Fill(dt);
  
              DataRow row = dt.NewRow();
              row["Familienname"] = "Mustermann";
              row["Eid"] = "1";
              row["Vorname"] = "Max";
              row["age"] = "20";
              dt.Rows.Add(row);
              da.Update(dt);
  
              conn.Close();
  
  
  
  
          }
          catch(Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
      }
  </code></pre>
  
  <p><strong>Question</strong>: Can I save the full DataGridView in my Database whenever the form is closing, or whenever I press a Button? I don´t want to insert the values with my code. The values should be read out of the Cells. So the user can change something on the UI and if he close the Form the Gridview will be saved in the MySql-Database.</p>
  
  <p>I know this website is not a "code it for me" Site, but I´m sure there is a method which can Insert all values in my MySql-Database! </p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>EDIT</strong>  I found this link: <a href="http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples-mysqldataadapter.html" rel="nofollow">MysqlLINK</a> But the sample programm still don´t work.</p>