I'm currently having a table named 'Events' where there are columns 'EquipApprovedDate', 'EquipCalDueDate', & 'ThemeColor'. I want to update the ThemeColor based on the remaining days left before the EquipCalDueDate.
Green = Good
Orange = Considered okay
Red = Critical
The total days will be different for every equipment registered on the calendar. It will be calculated by using this formula (totaldays = EquipCalDueDate - EquipApprovedDate). The remaining days will be calculated using this formula (remainingDays = EquipCalDueDate - DateTime.Now).
If remainingDays is more than 2/3 of totaldays it will be labelled 'green'.
If remainingDays is less than 2/3 of totaldays but more than 1/3 of totaldays it will be labelled 'orange'.
If remainingDays is less than 1/3 of totaldays it will be labelled 'red'.
I want to apply this whole process on table every time the page is loaded, specifically on each and every single row found in the database. Basically to collect the data and return it for every single row. For now it's only running partially where the column ThemeColor is being updated to 'green' for every single row no matter what. What is the correct SQL query?
I have attached my current work together for your view.
con.Open();
string yyy = "SELECT * FROM [Events]";
using (SqlCommand cmd = new SqlCommand(yyy, con))
{
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
String startdate = reader["EquipApprovedDate"].ToString();
DateTime Sdate = DateTime.Parse(startdate);
String enddate = reader["EquipCalDueDate"].ToString();
DateTime Edate = DateTime.Parse(enddate);
String themecolor = reader["ThemeColor"].ToString();
double totaldays = (Edate - Sdate).TotalDays;
double remainingDays = (Edate - DateTime.Now).TotalDays;
if (remainingDays > (totaldays * (2 / 3)))
{
string sqlCoC = "UPDATE Events SET ThemeColor = 'green'";
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MVCConnectionString"].ConnectionString))
{
SqlCommand coccmd = new SqlCommand(sqlCoC, con);
con.Open();
coccmd.ExecuteNonQuery();
con.Close();
}
//green = means good
}
else if ((remainingDays < (totaldays * (2 / 3))) && (remainingDays > (totaldays * (1 / 3))))
{
string sqlCoC = "UPDATE Events SET ThemeColor = 'orange'";
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MVCConnectionString"].ConnectionString))
{
SqlCommand coccmd = new SqlCommand(sqlCoC, con);
con.Open();
coccmd.ExecuteNonQuery();
con.Close();
}
//orange = considered okay
}
else if (remainingDays < (totaldays * (1 / 3)))
{
string sqlCoC = "UPDATE Events SET ThemeColor = 'red'";
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MVCConnectionString"].ConnectionString))
{
SqlCommand coccmd = new SqlCommand(sqlCoC, con);
con.Open();
coccmd.ExecuteNonQuery();
con.Close();
}
//red = critical
}
else { }
}
reader.Close();
con.Close();
}