Home > Net >  Reminder Application C# (Mongodb) : How to filter the data from mongodb and compare them specially d
Reminder Application C# (Mongodb) : How to filter the data from mongodb and compare them specially d

Time:05-10

Hi Guys im New to coding and i just want some help or advice on creating a simple c# winforms reminder app

I wanna create a reminder application which imputs date and time for the reminder with name and description.I actually wanna do this using timertick but i wanted to try it out first using just a button to check reminder time and date and compare with system time and date but its kinda hard for me to come up with the algorithm for this to work . And Finally if the reminderdate and remindertime is less than or equal to systemdate and system time Give a Message Box saying Reminder Alert and deleting than specific reminder. I kind of came up with most of it but im stuck with the logic where it compares the values.

using MongoDB.Driver;
using System;
using System.Linq;
using System.Windows.Forms;

namespace MongoDBTest
{
    public partial class Reminder : Form
    {
        string connectionString = "mongodb://localhost:27017";
        public string databaseName = "MongoDB";
        public string collectionName = "Reminders";
        IMongoCollection<remindermodel> reminderCollection;
        public Reminder()
        {
            InitializeComponent();

            timepicker.Format = DateTimePickerFormat.Time;

            timepicker.ShowUpDown = true;

            datepicker.Format = DateTimePickerFormat.Custom;
            datepicker.CustomFormat = "MM/dd/yyyy";

            var client = new MongoClient(connectionString);
            var db = client.GetDatabase(databaseName);
            reminderCollection = db.GetCollection<remindermodel>(collectionName);
        }



        private void button1_Click(object sender, EventArgs e)
        {

            DateTime setdate = datepicker.Value; // TRYING TO SET date only here but cudnt
            DateTime datetoday = DateTime.Now;

            DateTime dt = this.timepicker.Value;


            var settime = dt.TimeOfDay;
            var set = settime.ToString();
            var currenttime = datetoday.TimeOfDay;

            

            if ((datetoday < setdate) && (currenttime < settime))
            {
                MessageBox.Show("Reminder Set!!");
                var remindermodel = new remindermodel
                {
                    remindername = aname.Text,
                    reminderdate = setdate,
                    remindertime = TimeSpan.Parse(set),
                    reminderdescription = reminderdesc.Text,
                };
                label9.Text = setdate.ToString();
                label8.Text = set;

                reminderCollection.InsertOneAsync(remindermodel);
            }
            else
            {
                MessageBox.Show("Reminder Cannot be set!!");
            }
            
        }



        private void button2_Click(object sender, EventArgs e)
        {
            DateTime datenow = DateTime.Now;
            var timenow = datenow.TimeOfDay;

            

            var filtertime = Builders<remindermodel>.Filter.Gte(b => b.reminderdate, datenow);
            var filterDefinition = Builders<remindermodel>.Filter.Gte(b => b.reminderdate, datenow) & filtertime;
            var projection = Builders<remindermodel>.Projection.Exclude("_id").Include("reminderdate").Include("remindertime");
            var reminders = reminderCollection.Find(filterDefinition).Project<remindermodel>(projection)    
                .FirstOrDefault();

            var remindersdatagrid = reminderCollection.Find(filterDefinition).Project<remindermodel>(projection)
                .ToList();

            datagridview.DataSource = reminders;


            if(reminders != null)
            {
                label7.Text = reminders.reminderdate.ToString();
                label8.Text = reminders.remindertime.ToString();
                MessageBox.Show("Reminder found");
            }
            else
            {
                MessageBox.Show("Sorry No reminders");
            }

        }

        
    }
}

And My reminder model is

using System;

namespace MongoDBTest
{
    public class remindermodel
    {
        public string remindername { get; set; }

        public DateTime reminderdate { get; set; }

        public TimeSpan remindertime { get; set; }

        public string reminderdescription { get; set; }
    }
}

can someone help me with the logic to compare these times and dates. Cause even when setting time from datetimepicker it sets the value like 05/09/2022 18 39 PM. I cant seem to get rid of the last time part cause im getting the time separately from a timepicker.

CodePudding user response:

This is how i would go about it.To compare dates use The date property

var time_comparison_result = TimeSpan.Compare(settime,currenttime);
var date_comparison_result = DateTime.Compare(setdate.Date,datetoday.Date);

                if ((date_comparison_result>0) && 
                    (time_comparison_result>0))
                {
                   //Set reminder
                }
                else
                {
                    //MessageBox.Show("Reminder Cannot be set!!");
                }

CodePudding user response:

I'd propose to store the reminder date and time as a single field in MongoDB. This simplifies comparing because you only have to compare one field:

public class remindermodel
{
    public string remindername { get; set; }
    public DateTime reminderdate { get; set; }
    // Not needed: public TimeSpan remindertime { get; set; }
    public string reminderdescription { get; set; }
}

Then, when retrieving the date and the time from the UI, you can first get the date from the DatePicker and add the time from the TimePicker:

private void button1_Click(object sender, EventArgs e)
{
  DateTime setdate = datepicker.Value; 
  var timeOfDay = this.timepicker.Value.TimeOfDay;
  // .Date gets the date part from the DatePicker, 
  // Add adds the time that was selected in the TimePicker
  setDate = setDate.Date.Add(timeOfDay);
  // ... assign setDate to remindermodel.reminderdate later on

When filtering for reminders, you only need to compare the time field:

private void button2_Click(object sender, EventArgs e)
{
  DateTime datenow = DateTime.Now;
  // Not needed anymore: var filtertime = Builders<remindermodel>.Filter.Gte(b => b.reminderdate, datenow);
  // If you want to show reminders of the past, change comparison to Lte (Less than or equal)
  var filterDefinition = Builders<remindermodel>.Filter.Gte(b => b.reminderdate, datenow);
  // ...
  • Related