Reputation: 21
I am inserting some data from one file to another,first file has three different value records,when loop finish, when I see second file it has three records but with repeated with same value, can you help me out what is wrong with my code.
Loop from Old file to new file
foreach (DataRow drtableOld in orderTable.Rows)
{
command.CommandType = CommandType.Text;
command.CommandText = "insert into Data ([Terminal],[Token_no],[Date],[Time],[Mode]) values(?,?,?,?,?)";
command.Parameters.AddWithValue("@cTerminal", drtableOld["Terminal"]);
command.Parameters.AddWithValue("@cToken", drtableOld["Token"].ToString().PadLeft(6,'0') );
command.Parameters.AddWithValue("@cDate", drtableOld["Date"]);
command.Parameters.AddWithValue("@cTime", drtableOld["Time"]);
command.Parameters.AddWithValue("@cMode", "T");
command.Connection = yourConnectionHandler;
command.ExecuteNonQuery();
}
Upvotes: 0
Views: 25
Reputation: 62213
You need to clear your parameters at the beginning of the loop or create a new command. You can also move the assignment to the connection outside the loop because you are reusing the command in the loop.
command.Connection = yourConnectionHandler;
foreach (DataRow drtableOld in orderTable.Rows)
{
command.Parameters.Clear();
// rest of code unchanged
Upvotes: 2