title: How to loop through rows and columns in a DatagridView to insert their values in a mysql table
body: <p>Well, I have a bunch of .txt files that I'm importing in a <code>DataGridView</code> with vb.net, these files will always have different columns, just like these two examples:</p>
  <p>First and Second DataGridView
  <a href="https://i.stack.imgur.com/Af3op.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Af3op.jpg" alt="enter image description here" /></a></p>
  <p>And I'm using the next code to loop through the rows and columns of the DataGridView and also the code insert the data to a mysql table, now, the process I make, it's the next:</p>
  <ul>
  <li>I open the .txt file.</li>
  <li>I tried to insert the data with the First DataGridView, but with the error, the data it's not supposed to be inserted to the table, nevertheless it does it, and although it only has 4 colummns, when I check my database, it shows everything messed up, and it duplicates columns, like the lower image.</li>
  <li>And then I &quot;export&quot; them to the mysql table. And when I do that, I get the error in the upper image.</li>
  </ul>
  <p><a href="https://i.stack.imgur.com/7uGMQ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7uGMQ.jpg" alt="http://i.stack.imgur.com/7uGMQ.jpg" /></a></p>
  <p>Here's my code:</p>
  <pre><code>Private Sub ExportarToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExportarToolStripMenuItem.Click
  
      Dim conn As MySqlConnection = New MySqlConnection(&quot;Server=localhost;user=root;password=1234;database=chafa;port=3306&quot;)
      conn.Open()
  
      Dim comm As MySqlCommand = New MySqlCommand()
      comm.Connection = conn
  
     
  
      Dim col1, col2, col3, col4, col5, col6, col7, col8, col9, col10
         
      Dim tabla As New DataTable
  
  
      For i = 2 To DgvDatos.Rows.Add - 1 Step 1
          For j = 0 To Me.DgvDatos.Columns.Count - 1
  
              col1 = DgvDatos.Rows(i).Cells(j).Value()
              col2 = DgvDatos.Rows(i).Cells(j).Value()
              col3 = DgvDatos.Rows(i).Cells(j).Value()
              col4 = DgvDatos.Rows(i).Cells(j).Value()
              col5 = DgvDatos.Rows(i).Cells(j).Value()
              col6 = DgvDatos.Rows(i).Cells(j).Value()
              col7 = DgvDatos.Rows(i).Cells(j).Value()
              col8 = DgvDatos.Rows(i).Cells(j).Value()
              col9 = DgvDatos.Rows(i).Cells(j).Value()
              col10 = DgvDatos.Rows(i).Cells(j).Value()
  
  
              comm.CommandText = &quot;insert into ejemplo(col1,col2,col3,col4,col5,col6,col7,col8,col9,col10) values('&quot; &amp; col1 &amp;_ &quot;','&quot; &amp; col2 &amp; &quot;','&quot; &amp; col3 &amp; &quot;','&quot; &amp; col4 &amp; &quot;','&quot; &amp; col5 &amp; &quot;','&quot; &amp; col6 &amp; &quot;','&quot; &amp; col7 &amp; &quot;','&quot; &amp; col8 &amp; &quot;','&quot; &amp; col9 &amp; &quot;','&quot; &amp; col10 &amp; &quot;')&quot;
              comm.ExecuteNonQuery()
          Next
      Next
  
  
  
      MessageBox.Show(&quot;Datos Agregados Correctamente&quot;)
      conn.Close()
  
  End Sub
  </code></pre>
  <p>What am I doing wrong?</p>