Croxino
Croxino

Reputation: 33

Database into a list collection

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);
        }

    }

Upvotes: 0

Views: 73

Answers (2)

Creep
Creep

Reputation: 320

I think something like that would fit (import system.data.linq), launch check every minutes (regarding to precision needed):

    public IEnumerable<DateTime> DateTimes()
    {
        using(DataContext dc = new DataContext("constring"))
        {
            return dc.ExecuteQuery<DateTime>("SELECT Time FROM dbo.Planner");
        }
    }

    public void Check()
    {
        foreach(var dateTime in DateTimes())
        {
            if(dateTime.Minute==DateTime.Now.Minute && dateTime.Hour ==  DateTime.Now.Hour)
            {
                MessageBox.Show("Notification");
            }
        }
    }

Upvotes: 0

CDove
CDove

Reputation: 1950

Using the .ToString() method of an object can often present results that we would consider a "match", but a computer wouldn't. It's risky, especially if the database has padded fields or you are looking for a ":" but the field is really a count of seconds or is a Julian date.

Instead, I recommend that you compare two DateTime objects. Hopefully, the text you're getting back will cast cleanly:

foreach (string item in Time)
            {
                var n = Convert.ToDateTime(item);
                if (n.ToString("HH:mm") == DateTime.Now.ToString("HH:mm"))
                {
                    MessageBox.Show("Test");
                }
            }

Now I have two objects for comparison which I can ensure are giving me the exact same string format to represent an hour and a minute. There's a better way to do that, though.

 if (n.Hour == DateTime.Now.Hour && n.Minute == DateTime.Now.Minute)
                {
                    MessageBox.Show("Test");
                }

If you can compare that way instead of messing around with strings, it's always preferable.

Upvotes: 1

Related Questions