I'm able to add two different rows at the same time into the database with the Date value changing. The code is redundant and doesn't look good. Not sure how I can make it dynamic.
SqlCommand cmd = new SqlCommand("insert into Incomings (AspNetUsersId,IncType,IncDate,IncCost,IncFrequency) values (@AspNetUsersId, @IncType, @IncDate, @IncCost, @IncFrequency)");
SqlCommand cmd2 = new SqlCommand("insert into Incomings (AspNetUsersId,IncType,IncDate,IncCost,IncFrequency) values (@AspNetUsersId, @IncType, @IncDate, @IncCost, @IncFrequency)");
if (frequencyIncoming.Text == "Weekly")
{
DateTime newDate = DateTime.ParseExact(lblCalendar.Text, "dd/MM/yyyy", null);
cmd.Parameters.AddWithValue("@AspNetUsersId", userId);
cmd.Parameters.AddWithValue("@IncType", typeIncoming.Text);
cmd.Parameters.AddWithValue("@IncDate", lblCalendar.Text);
cmd.Parameters.AddWithValue("@IncCost", costIncoming.Text);
cmd.Parameters.AddWithValue("@IncFrequency", frequencyIncoming.Text);
cmd2.Parameters.AddWithValue("@AspNetUsersId", userId);
cmd2.Parameters.AddWithValue("@IncType", typeIncoming.Text);
cmd2.Parameters.AddWithValue("@IncDate", newDate.AddDays(7).ToString(@"dd\/MM\/yyyy"));
cmd2.Parameters.AddWithValue("@IncCost", costIncoming.Text);
cmd2.Parameters.AddWithValue("@IncFrequency", frequencyIncoming.Text);
}
loadDatabase(cmd);
loadDatabase(cmd2);
Response.Redirect(Request.Url.AbsoluteUri);
At the moment it works although the code isn't great and is redundant. It's a start. The only problem is if for e.g. I select the Daily Frequency and want to add it for 14 days, I would have to do 14 different select statements which is horrendous practice. Not sure how to tidy this up. Thanks in advance
forloop to insert the data.