private void btnShowGraph_Click(object sender, EventArgs e) { if (cbbFromMonth.Text == "Select Month") { MessageBox.Show("Please select a month for the From Month field.", "Incomplete Fields", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //---------------- // MONTHLY SALES | //---------------- if (radMonthly.Checked == true) { //Display relevant visuals and clear contents graphAnnually.Hide(); dgvAnnualSales.Hide(); graphMonthly.Series["Sales"].Points.Clear(); dgvMonthlySales_Left.Rows.Clear(); dgvMonthlySales_Right.Rows.Clear(); //Select Statement int fromMonth = DateTime.ParseExact(cbbFromMonth.Text, "MMMM", CultureInfo.InvariantCulture).Month; int fromYear = int.Parse(nupFromYear.Value.ToString()); DateTime fromDate = new DateTime(fromYear, fromMonth, 1); double[] monthlySales = new double[12]; //Values filled by SQL sql.SelectMonthlySales(fromDate, monthlySales); //Display results for (int i = 0; i < 12; i++) { DateTime dateToCheck = fromDate.AddMonths(i); string monthXValue = dateToCheck.ToString("MMMM"); graphMonthly.Series["Sales"].Points.AddXY(monthXValue, monthlySales[i]); if (i < 6) { dgvMonthlySales_Left.Rows.Add(monthXValue, monthlySales[i]); } else { dgvMonthlySales_Right.Rows.Add(monthXValue, monthlySales[i]); } } graphMonthly.Show(); dgvMonthlySales_Left.Show(); dgvMonthlySales_Right.Show(); } //--------------- // ANNUAL SALES | //--------------- if (radAnnually.Checked == true) { //Validation for date range (Already accounts for non-January-to-December fiscal years) int fromMonth = DateTime.ParseExact(cbbFromMonth.Text, "MMMM", CultureInfo.InvariantCulture).Month; int fromYear = int.Parse(nupFromYear.Text); int toMonth = DateTime.ParseExact(txtToMonth.Text, "MMMM", CultureInfo.InvariantCulture).Month; int toYear = int.Parse(txtToYear.Text); DateTime fromDate = new DateTime(fromYear, fromMonth, 1); DateTime toDate = new DateTime(toYear, toMonth, 1).AddMonths(1).AddDays(-1); //Set Day value as last day of the month //Reference: http://stackoverflow.com/questions/4127363/date-difference-in-years-c-sharp TimeSpan span = toDate - fromDate; DateTime zeroTime = new DateTime(1, 1, 1); //For calculating the date difference in years int range = (zeroTime + span).Year - 1; if (range < 3) { MessageBox.Show("Annual Sales reports cover at least 3 years worth of sales. Please adjust your dates.", "Date Range Too Short", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //Display relevant visuals and clear contents graphMonthly.Hide(); dgvMonthlySales_Left.Hide(); dgvMonthlySales_Right.Hide(); graphAnnually.Series["Sales"].Points.Clear(); dgvAnnualSales.Rows.Clear(); //Select Statement double[] annualSales = new double[range]; sql.SelectAnnualSales(fromDate, toDate, range, annualSales); //Display results for (int i = 0; i < range; i++) { DateTime dateToCheck = fromDate.AddYears(i); string yearXValue = dateToCheck.ToString("yyyy"); graphAnnually.Series["Sales"].Points.AddXY(yearXValue, annualSales[i]); dgvAnnualSales.Rows.Add(yearXValue, annualSales[i]); } graphAnnually.Show(); dgvAnnualSales.Show(); } }