When a user adds a school class to the system they choose the start and end date for the class. I have worked out how to calculate the number of days this is and store it in my database table 'Class' within the column 'TotalDayNumber'.
Currently when the user adds a class it just inserts one row into the database but I want to add the same number of rows for each individual day 'TotalDayNumber'.
E.g if the start date is 01/03/2017 - 15/03/17 - with the total number of days being 14 so I want to create 14 total rows to represent each day. 'Below is my INSERT query. I believe I require an IF statement but I don't know how to even begin it.
protected void insertbutton_Click(object sender, EventArgs e)
{
int? recurrencedata = Convert.ToInt32(ddlRecurrence.Text);
if (recurrencedata == 1)
{
int moduledata = Convert.ToInt32(ddlModule.Text);
DateTime startdatedata = Convert.ToDateTime(txtstartdate.Text);
DateTime enddatedata = Convert.ToDateTime(txtenddate.Text);
int classtypedata = Convert.ToInt32(ddlClassType.Text);
int roomcodedata = Convert.ToInt32(ddlRoomCode.Text);
int starttimedata = Convert.ToInt32(ddlStartClassTime.Text);
int endtimedata = Convert.ToInt32(ddlEndClassTime.Text);
startdatedata = DateTime.Parse(txtstartdate.Text).Date;
enddatedata = DateTime.Parse(txtenddate.Text).Date;
TimeSpan totaldays = enddatedata - startdatedata;
// This is rounding the TimeSpan to the day value only
string totalday = ((int)Math.Round(totaldays.TotalDays, MidpointRounding.AwayFromZero)).ToString();
string DefaultConnection = WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
SqlConnection myConnection = new SqlConnection(DefaultConnection);
myConnection.Open();
string query = "INSERT INTO Class (ModuleId, ClassTypeId, ClassScheduleStartTimeId, ClassScheduleEndTimeId, RoomCodeId, StartTime, EndTime, RecurrenceId, TotalDayNumber) VALUES ( @moduledata, @classtypedata, @starttimedata, @endtimedata, @roomcodedata, @startdatedata, @enddatedata, @recurrencedata, @totaldaynumberdata)";
SqlCommand myCommand = new SqlCommand(query, myConnection);
myCommand.Parameters.AddWithValue("@moduledata", moduledata);
myCommand.Parameters.AddWithValue("@classtypedata", classtypedata);
myCommand.Parameters.AddWithValue("@startdatedata", startdatedata);
myCommand.Parameters.AddWithValue("@enddatedata", enddatedata);
myCommand.Parameters.AddWithValue("@starttimedata", starttimedata);
myCommand.Parameters.AddWithValue("@roomcodedata", roomcodedata);
myCommand.Parameters.AddWithValue("@endtimedata", endtimedata);
myCommand.Parameters.AddWithValue("@recurrencedata", recurrencedata);
myCommand.Parameters.AddWithValue("@totaldaynumberdata", totalday);
myCommand.ExecuteNonQuery();
myConnection.Close();
SuccessPanel.Visible = true;
}
}
Aucun commentaire:
Enregistrer un commentaire