I'm trying to put the information I have in my database into a list, so it can compare the time specified in the database with the time now and show a notification.
So far its not quite working, I feel like i'm on the right track, could someone please take a look at my code?
private void timer1_Tick(object sender, EventArgs e)
{
try
{
DataTable dt = new DataTable();
SqlDataAdapter sqlDA = new SqlDataAdapter("SELECT Time FROM dbo.Planner", connectionString);
sqlDA.Fill(dt);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
List<string> Time = new List<string>();
if (dt.Rows.Count >= 0)
{
foreach (DataRow item in dt.Rows)
{
Time.Add(item["Time"].ToString());
Console.WriteLine(item[1]);
}
foreach (string item in Time)
{
if (item == DateTime.Now.ToString("HH:mm"))
{
MessageBox.Show("Test");
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
if (dt.Rows.Count >= 0)should also be> 0otherwise you'll be putting null rows into yourforeachloops should.Count == 0.