I want to make different entries for this same query but with hours and dates I ran into an error:
Parameters must be unique
Is there any way around it?
List<int> hoursList = new List<int>{1,2,3,4,5,6,7};
string connectionString = ConfigurationManager.ConnectionStrings["db"].ConnectionString;
using (var con = new SqlConnection(connectionString))
{
var query = @"INSERT INTO EmployeeTable (EmployeeID, ProjectID, CategoryID, SubCategoryID, Location, Date, Hours)
VALUES (@EmployeeID, @ProjectID, @CategoryID, @SubCategoryID, @Location, @Date, @Hours,)";
using(var cmd = new SqlCommand(query,con))
{
cmd.Parameters.AddWithValue("@EmployeeID",obj.EmployeeID);
cmd.Parameters.AddWithValue("@ProjectID", obj.ProjectID);
cmd.Parameters.AddWithValue("@CategoryID", obj.CategoryID);
cmd.Parameters.AddWithValue("@SubCategoryID", obj.SubCategoryID);
cmd.Parameters.AddWithValue("@Location", obj.Location);
for(int j = 0; j < hoursList.Count; j++)
{
cmd.Parameters.AddWithValue("@Hours", hoursList[j]);
cmd.Parameters.AddWithValue("@Date", DateTime.Now.AddDays(j).ToString("yyyy/MM/dd"));
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}