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();
}