protected void btnSelect_Click(object sender, EventArgs e) { DateTime startDate = DateTime.Today; DateTime endDate = DateTime.Today; #region Parse the selected school foreach (School school in AllSchools) { if (lstSchoolList.SelectedItem.Value == school.getGovIDAsString()) { selectedSchool = school; } } #endregion #region Parse the given date int startYear = int.Parse(from_year.SelectedValue); int startMonth = int.Parse(from_month.SelectedValue); int startDay = int.Parse(from_day.SelectedValue); if (startDay > DateTime.DaysInMonth(startYear, startMonth)) { startDay = DateTime.DaysInMonth(startYear, startMonth); } int endYear = int.Parse(to_year.SelectedValue); int endMonth = int.Parse(to_month.SelectedValue); int endDay = int.Parse(to_day.SelectedValue); if (endDay > DateTime.DaysInMonth(endYear, endMonth)) { endDay = DateTime.DaysInMonth(endYear, endMonth); } startDate = new DateTime(startYear, startMonth, startDay); endDate = new DateTime(endYear, endMonth, endDay); #endregion using (SqlConnection connection = new SqlConnection(dbConnectionString)) { AllSchools = School.loadAllSchools(connection); if (IsPostBack) { if (selectedSchool != null) { List <Student> DisplayedStudents = LSKY_INAC.loadStudentData(connection, selectedSchool, startDate, endDate); lblCount.Text = "Found " + DisplayedStudents.Count + " students."; if (DisplayedStudents.Count > 0) { tblResults.Visible = true; lnkCSVDownload.Visible = true; lnkCSVDownload.NavigateUrl = "INAC_CSV.aspx?schoolid=" + selectedSchool.getGovIDAsString() + "&from_year=" + startDate.Year + "&from_month=" + startDate.Month + "&from_day=" + startDate.Day + "&to_year=" + endDate.Year + "&to_month=" + endDate.Month + "&to_day=" + endDate.Day; } foreach (Student student in DisplayedStudents) { tblResults.Rows.Add(createStudentRow(student, startDate, endDate)); } } } } }
protected void Page_Load(object sender, EventArgs e) { DateTime startDate = DateTime.Today; DateTime endDate = DateTime.Today; /* Get data from the query string */ if (!String.IsNullOrEmpty(Request.QueryString["schoolid"])) { if ((!String.IsNullOrEmpty(Request.QueryString["from_year"])) || (!String.IsNullOrEmpty(Request.QueryString["from_month"])) || (!String.IsNullOrEmpty(Request.QueryString["from_day"]))) { if ((!String.IsNullOrEmpty(Request.QueryString["to_year"])) || (!String.IsNullOrEmpty(Request.QueryString["to_month"])) || (!String.IsNullOrEmpty(Request.QueryString["to_day"]))) { // Parse school int selectedSchoolID = -1; if (int.TryParse(Request.QueryString["schoolid"], out selectedSchoolID)) { // Parse from date int startYear = -1; int startMonth = -1; int startDay = -1; int.TryParse(Request.QueryString["from_year"], out startYear); int.TryParse(Request.QueryString["from_month"], out startMonth); int.TryParse(Request.QueryString["from_day"], out startDay); // Parse to date int endYear = -1; int endMonth = -1; int endDay = -1; int.TryParse(Request.QueryString["to_year"], out endYear); int.TryParse(Request.QueryString["to_month"], out endMonth); int.TryParse(Request.QueryString["to_day"], out endDay); if ( !( (startYear == -1) || (startMonth == -1) || (startDay == -1) || (endYear == -1) || (endMonth == -1) || (endDay == -1) ) ) { if (startDay > DateTime.DaysInMonth(startYear, startMonth)) { startDay = DateTime.DaysInMonth(startYear, startMonth); } if (endDay > DateTime.DaysInMonth(endYear, endMonth)) { endDay = DateTime.DaysInMonth(endYear, endMonth); } startDate = new DateTime(startYear, startMonth, startDay); endDate = new DateTime(endYear, endMonth, endDay); // Grab data using (SqlConnection connection = new SqlConnection(dbConnectionString)) { School selectedSchool = School.loadThisSchool(connection, selectedSchoolID); if (selectedSchool != null) { List <Student> DisplayedStudents = LSKY_INAC.loadStudentData(connection, selectedSchool, startDate, endDate); // Output a CSV file sendCSV(GenerateCSV(DisplayedStudents), "INAC_" + LSKYCommon.removeSpaces(selectedSchool.getName()) + "_" + startDate.Year + "-" + startDate.Month + "-" + startDate.Day + "_" + endDate.Year + "-" + endDate.Month + "-" + endDate.Day); } else { Response.Write("Invalid school specified"); } } } else { Response.Write("Invalid date specified<br>"); Response.Write(" From Year: " + startYear + "<br>"); Response.Write(" From Month: " + startMonth + "<br>"); Response.Write(" From Day: " + startDay + "<br>"); Response.Write(" To Year: " + endYear + "<br>"); Response.Write(" To Month: " + endMonth + "<br>"); Response.Write(" To Day: " + endDay + "<br>"); } } else { Response.Write("Invalid school ID"); } } else { Response.Write("\"To\" data not valid"); } } else { Response.Write("\"From\" data not valid"); } } else { Response.Write("School ID not specified"); } Response.End(); }