Croxino
Croxino

Reputation: 33

Send notification on Time/Date from database

I'm trying to figure out how to make my application send a notification on the specified date/time which it retrieves from my database.

example

it does not need to do anything complicated like a pop-up/sound notification it just needs to send a simple message.

my code so far.

    SqlConnection connectionString = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Goeli\Desktop\Feedr OIS\DateTimePlanner2\DateTimePlanner2\Database1.mdf;Integrated Security=True");

    public Form1()
    {
        InitializeComponent();
        dateTimePicker1.Format = DateTimePickerFormat.Custom;
        timePicker2.Format = DateTimePickerFormat.Custom;
        dateTimePicker1.CustomFormat = "MM/dd/yyyy";
        timePicker2.CustomFormat = "HH:mm";
        DisplayData();

    }

    private void button1_Click(object sender, EventArgs e)
    {

        try
        {
            if (textBox1.Text != "")
            {

                SqlCommand cmd = new SqlCommand("INSERT INTO dbo.Planner(Name, Date, Time) VALUES(@Name, @Date, @Time)", connectionString);
                connectionString.Open();
                cmd.Parameters.AddWithValue("@Name", textBox1.Text);
                cmd.Parameters.AddWithValue("@Date", dateTimePicker1.Text);
                cmd.Parameters.AddWithValue("@Time", timePicker2.Text);
                cmd.ExecuteNonQuery();
                connectionString.Close();
                MessageBox.Show("Successfully Planned");
                DisplayData();
                //ClearData();
            }
            else
            {
                MessageBox.Show("Please provide a name");
            }
        }
        catch (Exception)
        {

            MessageBox.Show("Cannot have duplicate name");
        }
    }

    private void DisplayData()
    {
        connectionString.Open();
        DataTable dt = new DataTable();
        SqlDataAdapter adapt = new SqlDataAdapter("select * from dbo.Planner", connectionString);
        adapt.Fill(dt);
        dataGridView1.DataSource = dt;
        connectionString.Close();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("DELETE FROM dbo.Planner WHERE Name=@Name", connectionString);
        try
        {
            if (textBox1.Text != "")
            {                   
                connectionString.Open();
                cmd.Parameters.AddWithValue("@Name", textBox1.Text);
                cmd.ExecuteNonQuery();
                connectionString.Close();
                MessageBox.Show(" Successfully Deleted");
                DisplayData();
            }

            else
            {
                MessageBox.Show("Please provide the name to delete");
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }       
    }
}

}

Upvotes: 2

Views: 890

Answers (1)

Yuriy Tseretyan
Yuriy Tseretyan

Reputation: 1716

As the fastest and simplest solution I would propose to use

  1. A Timer that raises events, for example, every minute (or second, depends on granularity you need to achieve) and reads a schedule - collection of dates\times with messages.
  2. A separate thread (or a timer) that reads (polls) your database every X seconds and maintain the schedule (adds\removes events).

Some simple synchronization needs to be involved.

As an alternative you can use 3rd party libraries, like FluentScheduler. For more heavyweight solutions if you need consistency across many instances of you application, consider Quartz.NET

Upvotes: 1

Related Questions