0

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

    }
7
  • 4
    "So far its not quite working". Elaborate. Commented Jan 5, 2017 at 15:23
  • if (dt.Rows.Count >= 0) should also be > 0 otherwise you'll be putting null rows into your foreach loops should .Count == 0. Commented Jan 5, 2017 at 15:27
  • @rory.ap I'm not sure if the code is right, am I even populating my List with the data from my database? and how do I make the comparison of checking time. Commented Jan 5, 2017 at 15:31
  • What are the results when you run the code? What is happening that you don't like? Commented Jan 5, 2017 at 15:32
  • Have you tried debugging to see what its doing? Stepping through the code line by line, observing the value of the variables? Commented Jan 5, 2017 at 15:41

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This worked!!!!! thank you so much!, I also need to make it check the date so it needs to check the Time + Date. Could you also help me with this? heres the code I have so far for the date pastebin.com/KxtqHp1E
DateTime has all sorts of methods for this sort of thing. You can use .Day, .Month, .Year, etc. I think the way for you would be to use: DateTime.ToShortDateString(); It's a string comparison, but since it'll come from two datetime objects using the same method, that's fine. If you want to compare the dates and times in the same operation, just compare DateTime n to DateTime.Now without using any of the properties.
0

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

4 Comments

What is this answering?
What is wrong with this answer ? (I try to be constructive, not a trolling question)
What is wrong with this answer is that we don't even know what the OP is asking, so how can we answer it? This isn't a general code review site; there must be a problem present for which we might be able to help.
By reading his code, I think he try to check in database if there's kind of notification and show a messagebox if there is one. I just rewrite what he tried.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.