good day, i am looping through a gridview control like this for example
+--------------+---------+---------+---------+---------+---------+---------+
| Values | Col1 | Col2 | Col3 | Col4 | Col5 | Col6 |
+--------------+---------+---------+---------+---------+---------+---------+
| Row1 | 1 | 2 | 3 | 4 | 5 | 6 |
+--------------+---------+---------+---------+---------+---------+---------+
| Row2 | 7 | 8 | 9 | 10 | 11 | 12 |
+--------------+---------+---------+---------+---------+---------+---------+
| Row3 | 13 | 14 | 15 | 16 | 17 | 18 |
+--------------+---------+---------+---------+---------+---------+---------+
| Row4 | 19 | 20 | 21 | 22 | 23 | 24 |
+--------------+---------+---------+---------+---------+---------+---------+
i'm looping through it and retrieving the values in the table below using these lines of code
MySqlConnection conn = new MySqlConnection(connection);
MySqlCommand cmd = new MySqlCommand("insert into databasename.tablename (Rows, Columns) values (@rows, @columns)", conn);
MySqlDataReader read;
conn.Open();
cmd.Parameters.Add("@rows", MySqlDbType.VarChar);
cmd.Parameters.Add("@columns", MySqlDbType.VarChar);
foreach(GridViewRow j in gridview1.Rows)
{
for (int i = 0; i < gridview1.Rows.Count; i++)
{
for (int k = 1; k < j.Cells.Count; k++)
{
label1.Text += gridview1.HeaderRow.Cells[k].Text + " ";
label1.Text += gridview1.Rows[i].Cells[k].Text +", ";
cmd.Parameters["@rows"].Value = excel.Rows[i].Cells[0].Text;
cmd.Parameters["@columns"].Value = label1.Text;
}
read = cmd.ExecuteReader();
read.Close();
test.Text = "";
}
}
conn.Close();
, lets assume the table below is a database table, this is how the loop results gets saved in the database
+--------------+-------------------------------------------------------+
| Rows | Columns |
+--------------+-------------------------------------------------------+
| Row1 | col1 1, col2 2, col3 3, col4 4, col5 5, col6 6, |
+--------------+-------------------------------------------------------+
| Row2 | col1 7, col2 8, col3 9, col4 10, col5 11, col6 12, |
+--------------+-------------------------------------------------------+
| Row3 | col1 13, col2 14, col3 15, col4 16, col5 17, col6 18, |
+--------------+-------------------------------------------------------+
| Row4 | col1 19, col2 20, col3 21, col4 22, col5 23, col6 24, |
+--------------+-------------------------------------------------------+
but i get this error after the loop loops through the table and inserts all records into the database
"Duplicate entry 'Row1' for key 'PRIMARY'"