Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm doing this program where I have this list of products (they are all in a database and are connected to the program through a dataset), and each one has a respective expiration date.

Now, I have this DateTimePicker, which is a Calendar linked to a database and from it you can change the expiration date (it also shows the actual one). What I need to know is how do I get the program to give me a warning when there is a product almost expiring ( something like, if x product expiration =< 10 days then msgbox("product x is almost expiring")).

If you didn't understand what I meant, say it and I'll explain it better.
Posted
Comments
OriginalGriff 3-Nov-10 16:47pm    
Don't post an answer - use a comment on an answer instead.
If you post an answer, the only person who could get a notification is you.
If you post a comment, then the person you are commenting to gets a notification - like this one.

You can simply use the AddDays method of DateTime and check like: expirationDate < DateTime.Now.AddDays(10)

http://msdn.microsoft.com/en-us/library/system.datetime.adddays.aspx[^]

Good luck!
 
Share this answer
 
Comments
OriginalGriff 3-Nov-10 16:45pm    
"I have 3 words for you!

I... love... you!

Many thanks to you sir"

Posted as an answer by the OP.
To add to EF Nijboers answer, don't repeatedly use DateTime.Now. Take a copy, and use that instead:
DateTime expiryDate = DateTime.Now.AddDays(10);
foreach(Product p in products)
   {
   if (p.ExpiryDate >= expiryDate)
      {
      ...
      }
   }
The reason for this is that each time to use DateTime.Now, it creates a new instance with the current date and time. In this example it makes no difference (except to waste a little time and memory), but it can cause intermittent bugs when the day rolls over between checks.
It is good practice to use DateTime.Now as few times as possible to avoid this.
 
Share this answer
 
That's a bit confusing to me Griff. Thanks for the help though :thumbsup:
 
Share this answer
 
Comments
Hiren solanki 3-Nov-10 23:22pm    
wrong place for comment, better comment inside answer of the respective.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900