title: Passing a Datatable From One Form to Another Form
body: <p>I have a form1 and form2.  Form2 is opened from form1, it has a dgv after filling it, it's copied to a datatable on a click button, and form2 is closed or visible = false.  I passed the datatable to form1 but it's giving me a null reference.  Here is my code in form2:</p>
  
  <pre><code>private void button1_Click(object sender, EventArgs e)
          {
              perclothesdescriptionDT = new DataTable();
  
              foreach (DataGridViewColumn col in dataGridView3.Columns)
              {
                  perclothesdescriptionDT.Columns.Add(col.HeaderText);
              }
  
              foreach (DataGridViewRow row in dataGridView3.Rows)
              {
                  DataRow dRow = perclothesdescriptionDT.NewRow();
                  foreach (DataGridViewCell cell in row.Cells)
                  {
                      dRow[cell.ColumnIndex] = cell.Value;
                  }
                  perclothesdescriptionDT.Columns.RemoveAt(0);
                  perclothesdescriptionDT.Rows.Add(dRow);
              }
              this.Visible = false;
  
          }
  
          public DataTable mydt
          {
              get
              {
                  return perclothesdescriptionDT;
              }
          }
  </code></pre>
  
  <p>In form1:</p>
  
  <pre><code>form2= new Form2();
  
                  cmd1.CommandText = "insert into [dbo].[personClothesDesc](upperPart, lowerPart, belt, socks, shoes, differentSigns) values (@upperPart, @lowerPart, @belt, @socks, @shoes, @differentSigns)";
                          for (int i = 0; i &lt; form2.mydt.Rows.Count; i++)
                          {
                              cmd1.Parameters.Clear();
                              cmd1.Parameters.AddWithValue("@upperPart", form2.mydt.Rows[i].ItemArray.GetValue(5).ToString());
                              cmd1.Parameters.AddWithValue("@lowerPart", form2.mydt.Rows[i].ItemArray.GetValue(4).ToString());
                              cmd1.Parameters.AddWithValue("@belt", form2.mydt.Rows[i].ItemArray.GetValue(3).ToString());
                              cmd1.Parameters.AddWithValue("@socks", form2.mydt.Rows[i].ItemArray.GetValue(2).ToString());
                              cmd1.Parameters.AddWithValue("@shoes", form2.mydt.Rows[i].ItemArray.GetValue(1).ToString());
                              cmd1.Parameters.AddWithValue("@differentSigns", form2.mydt.Rows[i].ItemArray.GetValue(0).ToString());
                              cmd1.ExecuteNonQuery();
                          }
  </code></pre>