Hi...new here. :) Trying a sql insert inside a foreach loop, getting "Variable name has already been declared...must be unique" error. I think the structure of my code or my parameter statements are incorrect.
Any help would be greatly appreciated! Thanks!
// open connection
mySqlConnection.Open();
//enumerate the items in the list
foreach (SPListItem curItem in curItems)
{
string sql = "INSERT INTO ImportNotifications";
sql += " (SharepointID,Title,WorkOrderNumber,Status,Department,ClientName,WorkOrderDueDate,CreatedDate,CreatedBy,ModifiedDate,ModifiedBy,Late,LateReview,TrueLate,Body)";
sql += " VALUES (@ID,@Title,@WorkOrderNum,@Status,@Department,@ClientName,@WorkOrderDue,@Created,@Author,@Modified,@Editor,@Late,@LateReview,@TrueLate,@Body)";
mySqlCommand.CommandText = sql;
mySqlCommand.Parameters.Add(new SqlParameter("@ID", SqlDbType.Float)).Value = curItem["ID"];
mySqlCommand.Parameters.Add(new SqlParameter("@Title", SqlDbType.NVarChar, 200)).Value = curItem["Title"].ToString();
mySqlCommand.Parameters.Add(new SqlParameter("@WorkOrderNum", SqlDbType.NVarChar, 50)).Value = curItem["Work_x0020_Order_x0020_Number"].ToString();
mySqlCommand.Parameters.Add(new SqlParameter("@Status", SqlDbType.NVarChar, 50)).Value = curItem["Status"].ToString();
mySqlCommand.Parameters.Add(new SqlParameter("@Department", SqlDbType.NVarChar, 100)).Value = curItem["Department"].ToString();
mySqlCommand.Parameters.Add(new SqlParameter("@ClientName", SqlDbType.NVarChar, 255)).Value = curItem["Client_x0020_Name"].ToString();
mySqlCommand.Parameters.Add(new SqlParameter("@WorkOrderDue", SqlDbType.DateTime)).Value = curItem["Work_x0020_Order_x0020_Due_x0020"];
mySqlCommand.Parameters.Add(new SqlParameter("@Created", SqlDbType.DateTime)).Value = curItem["Created"];
mySqlCommand.Parameters.Add(new SqlParameter("@Author", SqlDbType.NVarChar, 50)).Value = curItem["Author"].ToString();
mySqlCommand.Parameters.Add(new SqlParameter("@Modified", SqlDbType.DateTime)).Value = curItem["Modified"];
mySqlCommand.Parameters.Add(new SqlParameter("@Editor", SqlDbType.NVarChar, 50)).Value = curItem["Editor"];
mySqlCommand.Parameters.Add(new SqlParameter("@Late", SqlDbType.NVarChar, 50)).Value = curItem["Late"];
mySqlCommand.Parameters.Add(new SqlParameter("@LateReview", SqlDbType.NVarChar, 50)).Value = curItem["Late_x0020_Review"];
//mySqlCommand.Parameters.Add(new SqlParameter("@ArchiveDate", SqlDbType.DateTime)).Value = curItem["Archive_x0020_Date"];
mySqlCommand.Parameters.Add(new SqlParameter("@TrueLate", SqlDbType.NVarChar, 50)).Value = curItem["TrueLate"];
mySqlCommand.Parameters.Add(new SqlParameter("@Body", SqlDbType.NVarChar, -1)).Value = curItem["Body"].ToString();
//mySqlCommand.Parameters.Add(new SqlParameter("@Expires", SqlDbType.DateTime)).Value = curItem["Expires"];
// execute the command
mySqlCommand.ExecuteNonQuery();
// write inserted titles to screen
Console.Write(curItem["Title"].ToString());
}
// close the connection
mySqlConnection.Close();
Thanks again for any help!