I cannot get my desired data from a DataGridView
. It only exports the first row of data.
Database:
acct_no | first_name | last_name | mid_name
-----------------------------------------------------------
201 | first1 | last1 | mid1
202 | first2 | last2 | mid2
203 | first3 | last3 | mid2
-----------------------------------------------------------
But when I run my code the result is:
acct_no | first_name | last_name | mid_name
-----------------------------------------------------------
201 | first1 | last1 | mid1
202 | first1 | last1 | mid2
203 | first1 | last1 | mid2
-----------------------------------------------------------
It didn't loop to all rows. Here is my code:
for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
{
foreach (var cell in dataGridView1.Rows[i].Cells)
{
command.CommandText = "INSERT INTO MEMBER (ACCT_NO, LAST_NAME, FIRST_NAME, MID_NAME) VALUES(@ACCT_NO, @LAST_NAME, @FIRST_NAME, @MID_NAME)";
command.Parameters.AddWithValue("@ACCT_NO", dataGridView1.Rows[i].Cells[0].Value);
command.Parameters.AddWithValue("@LAST_NAME", dataGridView1.Rows[i].Cells[1].Value);
command.Parameters.AddWithValue("@FIRST_NAME", dataGridView1.Rows[i].Cells[2].Value);
command.Parameters.AddWithValue("@MID_NAME", dataGridView1.Rows[i].Cells[3].Value);
}
}
Problem : As I can not see your ExecuteNonQuery()
statement in the loop, I suspect that you are executing the query at the end of the loop.
EDIT 1: As @AbZy suggested from comments your foreach
loop does not make any sense because you are adding all cell
values from the current row
. So remove your foreach
loop.
Assuming you are are using OleDb, I don't think OleDb
supports Named Parameters
. So instead of parameter names provide the question marks in your query.
Solution1: Execute the command.ExecuteNonQuery();
statement inside the loop and clear the parameters.
Just add following two statements to your code:
command.ExecuteNonQuery();
command.Parameters.Clear();
Complete Code:
for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
{
command.CommandText = "INSERT INTO RP_CMG01 (ACCT_NO, LAST_NAME, FIRST_NAME,
MID_NAME) VALUES(?, ?, ?, ?)";
command.Parameters.AddWithValue("@ACCT_NO", dataGridView1.Rows[i].Cells[0].Value);
command.Parameters.AddWithValue("@LAST_NAME", dataGridView1.Rows[i].Cells[1].Value);
command.Parameters.AddWithValue("@FIRST_NAME", dataGridView1.Rows[i].Cells[2].Value);
command.Parameters.AddWithValue("@MID_NAME", dataGridView1.Rows[i].Cells[3].Value);
command.ExecuteNonQuery();
command.Parameters.Clear();
}
Try this:
for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
{
foreach (var cell in dataGridView1.Rows[i].Cells)
{
command.CommandText = "INSERT INTO RP_CMG01 (ACCT_NO, LAST_NAME, FIRST_NAME, MID_NAME) VALUES(@ACCT_NO, @LAST_NAME, @FIRST_NAME, @MID_NAME)";
command.Parameters.AddWithValue("@ACCT_NO", dataGridView1.Rows[i].Cells[0].Value);
command.Parameters.AddWithValue("@LAST_NAME", dataGridView1.Rows[i].Cells[1].Value);
command.Parameters.AddWithValue("@FIRST_NAME", dataGridView1.Rows[i].Cells[2].Value);
command.Parameters.AddWithValue("@MID_NAME", dataGridView1.Rows[i].Cells[3].Value);
command.ExecuteNoQuery();
command.Parameters.Clear();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.