I have some working code but it takes a long time to return the results to a textbox. The output could take up to 20 seconds to output, and I don't have much data to sift through.
I get the correct output but it is not working good.
Global variable:string newLine = Environment.NewLine;
public List<string> AppointmentTypesForReport(int month)
{
List<string> appointments = new List<string>();
string CS = ConfigurationManager.ConnectionStrings["U04i5a"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(CS))
{
try
{
con.Open();
MySqlCommand cmd = con.CreateCommand();
cmd.CommandText = "SELECT type FROM appointment WHERE MONTH(start) = @month";
cmd.Parameters.AddWithValue("@month", month);
cmd.ExecuteNonQuery();
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
appointments.Add(reader["type"].ToString());
}
}
}
catch (Exception)
{
throw;
}
}
return appointments;
}
private void TypesByMonthRadioButton_CheckedChanged(object sender, EventArgs e)
{
ReportsTextBox.Clear();
//resultTextBox.Text = string.Empty;
var typesOutput = "";
for (int m = 1; m <= 6; m++)
{
List<string> list = AppointmentTypesForReport(m);
var j = from t in list
group t by t into g
let totalNumber = g.Count()
orderby totalNumber descending
select new { Returned = g.Key, Tally = totalNumber };
typesOutput += newLine + DateTimeFormatInfo.CurrentInfo.GetMonthName(m) + newLine;
//convert to lambda
foreach (var t in j)
{
typesOutput += "Type of appointment scheduled: " + t.Returned + " " + "-Appointments of this type: " + t.Tally + newLine;
}
}
ReportsTextBox.Text = typesOutput;
}
When the code runs, the following is the output in the text box.
January Type of appointment scheduled: xyz -Appointments of this type: 2
February
March
April
May Type of appointment scheduled: General Doctor -Appointments of this type: 1
June Type of appointment scheduled: General Doctor -Appointments of this type: 1