title: How to save this table in C#?
body: <p>I have problem to save tables in the database. Im new in SQL and for a first time i use 2 tables. The results appears in the DataGridView but cant save them to the Database. I try many ways, but i cant save the Datagrid to the Database. </p>
  
  <p>Basically i try to export the results from DataGridView3 in another table to make them apear to a report on Form2.</p>
  
  <pre><code>    using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  using System.Data.SqlClient;
  
  namespace TestDB2
  {
      public partial class Form1 : Form
      {
  
          Form2 fr2 = new Form2();
  
          SqlConnection cn = new SqlConnection();
          // SqlCommand cmd = new SqlCommand();
          SqlDataReader dr;
  
          public Form1()
          {
              InitializeComponent();
          }
  
          private void Form1_Load(object sender, EventArgs e)
          {
  
              cn.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;Connect Timeout=30";
              // TODO: This line of code loads data into the 'database1DataSet.Table1' table. You can move, or remove it, as needed.
              this.table1TableAdapter.Fill(this.database1DataSet.Table1);
              // TODO: This line of code loads data into the 'database1DataSet.Table2' table. You can move, or remove it, as needed.
              this.table2TableAdapter.Fill(this.database1DataSet.Table2);
              textBox1.Text = monthCalendar1.TodayDate.ToString();
  
  
          }
  
          private void button1_Click(object sender, EventArgs e)
          {
              //  timer1.Enabled = true;
  
              database1DataSet.Table2.Clear();
  
  
              try
              {
  
  
                  dataGridView2.Rows.Clear();
                  // cmd.CommandText = "Select [First Last Name] from Custumers where Arrival=@arr";
                  SqlCommand cmd = new SqlCommand("Select * from Table1 where Date2 &gt;= @arr");
                  cmd.Connection = cn;
                  cmd.Parameters.AddWithValue("@arr", monthCalendar1.SelectionStart.Date);
                  cmd.Parameters.AddWithValue("@arr1", monthCalendar1.SelectionStart.Date.AddDays(1));
                  cn.Open();
                  dr = cmd.ExecuteReader();
  
                  if (dr.HasRows)
                  {
  
                      while (dr.Read())
                      {
  
                          dataGridView2.Rows.Add(dr[0].ToString(), dr[1].ToString(), dr[2].ToString(), dr[3].ToString());
  
  
                          int i = Convert.ToInt32(dr[0]);
                          string q = Convert.ToString(dr[1]);
                          DateTime w = Convert.ToDateTime(dr[2]);
                          DateTime r = Convert.ToDateTime(dr[3]);
                          this.database1DataSet.Table2.Rows.Add(i, q, w, r);
  
                          // this.database1DataSet.Table2.AddTable2Row(i, q, w, r).AcceptChanges();
                          // this.database1DataSet.Table2.GetChanges();
  
                          // cmd.CommandText = "INSERT into Table2 (Id,Name,Date1,Date2) VALUES (@ar,@ar1,@ar2,@ar3)";
  
                      }
  
  
  
                  }
                  dr.Close();
  
  
  
                  /*
                  for (int i = 0; i &lt; dataGridView2.Rows.Count; i++)
                  {
                      cmd.CommandText = @"INSERT INTO Table2 (Id) VALUES ('" + dataGridView2.Rows[i].Cells["ID"].Value + "')";
                      cmd.ExecuteNonQuery();
                  }
                  this.Validate();
                  */
                  this.Validate();
                  cn.Close();
                 // textBox1.Text = dataGridView3.Rows[2].Cells[1].Value.ToString();
                //  fr2.ShowDialog();
  
              }
              catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); cn.Close(); }
          }
  
          private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
          {
              textBox1.Text = monthCalendar1.SelectionStart.Date.ToString();
          }
  
          private void timer1_Tick(object sender, EventArgs e)
          {
  
          }
  
          private void button2_Click(object sender, EventArgs e)
          {
  
              try
              {
                  string sql = "INSERT INTO Table2 (Id, Name, Date1, Date2) VALUES (@A, @B, @C, @D)";
                  using (SqlConnection conn = cn)
                  {
                      conn.Open();
                      for (int i = 0; i &lt; dataGridView3.Rows.Count; i++)
                      {
  
                          SqlCommand cmd = conn.CreateCommand();
                          cmd.CommandText = sql;
                          cmd.Parameters.AddWithValue("@A", dataGridView3.Rows[i].Cells[0].Value.ToString());
                          cmd.Parameters.AddWithValue("@B", dataGridView3.Rows[i].Cells[1].Value.ToString());
                          cmd.Parameters.AddWithValue("@C", dataGridView3.Rows[i].Cells[2].Value.ToString());
                          cmd.Parameters.AddWithValue("@D", dataGridView3.Rows[i].Cells[3].Value.ToString());
                          cmd.ExecuteNonQuery();
  
                      }
                      conn.Close();
                  }
  
  
              }
              catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
  
          }
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/QG8n0.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QG8n0.jpg" alt="enter image description here"></a></p>