How to check expired date in my database using c# windows form

BgnerNprg207 286 Reputation points
2024-06-16T13:14:55.16+00:00

Good day, I want to check the expiration date on my database. Now if my Form1 running and detect the expiration date in my database the notification will show.

private void NotificationOfForm()
{
 DateTime date = DateTime.Now;
            MySqlCommand cmd1 = new MySqlCommand("SELECT * FROM db_vehicle_maintenance_schedule WHERE schedule >= DATE_SUB(NOW(), INTERVAL 1 DAY)", DBConnection.con);
            MySqlDataReader reader = cmd1.ExecuteReader();
            //reader.Read();
            if (reader.HasRows)
            {
                //MessageBox.Show("expire");
                notification("WARNING", "Attention! A vehicle schedule for \nmaintenance is detected. \nPlease check your car in your garage.");
            }
}

Please help me or else you can change my code to fix my error.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,561 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hongrui Yu-MSFT 780 Reputation points Microsoft Vendor
    2024-06-17T06:44:51.2833333+00:00

    Hi,@BgnerNprg207. Welcome to Microsoft Q&A. 

    Solution:

    1.Use the timer to determine whether it has timed out in real time.

    2.Use SQL query to determine whether it has timed out

    3.When it times out, use NotifyIcon to notify

     

    Reference Code:

    
        public partial class Form1 : Form
    
        {
    
            private Timer timer;
    
            private NotifyIcon notification1;
    
            public Form1()
    
            {
    
                InitializeComponent();
    
     
    
                //Set the timer
    
                timer = new Timer()
    
                {
    
                    Enabled = true,
    
                    Interval = 3000,  //How often to execute in milliseconds
    
                };
    
                timer.Tick += _timer_Tick;
    
     
    
                //Setting up Notifier
    
                notification1 = new NotifyIcon();
    
                notification1.Visible = true;
    
                notification1.Icon = SystemIcons.Warning;  // The Icon property sets the icon that will appear in the systray for this application.
    
            }
    
     
    
            private void _timer_Tick(object sender, EventArgs e)
    
            {
    
                using (MySqlConnection conn = new MySqlConnection("  "))  //Write your database connection string
    
                {          
    
                    conn.Open();
    
                    MySqlCommand cmd1 = new MySqlCommand("SELECT * FROM db_vehicle_maintenance_schedule WHERE schedule >= DATE_SUB(NOW(), INTERVAL 1 DAY)", conn);
    
                    MySqlDataReader reader = cmd1.ExecuteReader();
    
                    if (reader.HasRows)
    
                    {
    
                        notification1.ShowBalloonTip(50000, "Attention!", "A vehicle schedule for \nmaintenance is detected. \nPlease check your car in your garage.", ToolTipIcon.Warning);
    
                    }
    
                }    
    
            }
    
        }
    
    

    Attach:

    1.If you can tell me in more detail how schedule and INTERVAL 1 DAY are designed, I will be able to more accurately determine whether the following statement will work correctly

    
    MySqlCommand cmd1 = new MySqlCommand("SELECT * FROM db_vehicle_maintenance_schedule WHERE schedule >= DATE_SUB(NOW(), INTERVAL 1 DAY)", conn);
    
    

    2.NotifyIcon reference documentation: NotifyIcon Class (System.Windows.Forms) | Microsoft Learn

      Optimization suggestions:

    In this process, the timer executes SQL multiple times, and the SQL query consumes too much performance.

    Is it possible to execute SQL only once to query schedule and INTERVAL 1 DAY, and combine DateTime date = DateTime.Now in the application to determine whether it has timed out?

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful