I’m using sql server 2008 and trying to show a notification when a user’s last login was earlier than the date of the last message added to the database.
I’m using asp:panel to show hide the notification text and it works just fine, but for some reason I can’t figure out why the Compare method I’m using isn’t returning the expected result.
For example, when I set the user login to 01/05/2013 and the most recent date in the database is 01/04/2013, I expect the result to be a greater than zero int and escape the if condition. That, unfortunately, doesn’t happen and the method always goes into the if condition.
I think it may have something to do with the casting from Date to DateTime; ideas, please?
DateTime userEntry = new DateTime(2012, 01, 05);
DateTime lastMsg = new DateTime();
lastMsg = Convert.ToDateTime(ds.Tables[0].Rows[0]["Date"]);
int compared = DateTime.Compare(userEntry, lastMsg);
if (compared < 0) { notification.Visible = true; }
You mention that you are trying to compare “01/05/2013”, yet, the code is generating a 2012 date:
I suspect you really meant: