I am developing a system reminder application where the date n time are inserted along with a message in the database.when the datetime from the database matches with the current system datetime,it should display an alertbox.
I am inserting date and time with datetimepicker.
i will be able to get current datetime via datetime.now method but how should i match it with the database?

Recommended Answers

All 6 Replies

DateTime has a Compare method.
DateTime also has some operator overloads like == to test two dates for equality.
There is also the TimeSpan structure.

As ddanbe said. I will only show an example:

DateTime dateNow = DateTime.Now;
            // I will add just an example dateTime:
            DateTime dateFromDataBase = new DateTime(2011, 3, 21);

            //comparing both dates:
            if (dateNow.Date == dateFromDataBase.Date)
            {
                //if both dates are equal do code in here:
            }
            else
            {
                //if not equal, do in here (if needed)
            }

Mitja

As ddanbe said. I will only show an example:

DateTime dateNow = DateTime.Now;
            // I will add just an example dateTime:
            DateTime dateFromDataBase = new DateTime(2011, 3, 21);

            //comparing both dates:
            if (dateNow.Date == dateFromDataBase.Date)
            {
                //if both dates are equal do code in here:
            }
            else
            {
                //if not equal, do in here (if needed)
            }

Mitja

How to get that datefromdatabase ? is there any syntax for doing that.Do i have to run query through sqlcommand or sqlcommandbuuilder and store in that variable.
My table consists of 3 columns : ID,datetime,message n ID is set to primary key.

Sure. There is no other way.
YOu have to use sql query (SELECT x FROM y WHERE a = b) if condition is needed.
All with a sqlCommand help (sqlCommandBuilder is not needed here).

"select * from table_name where OrderDate<'" + dateTo + "' and OrderDate> '" + dateFrom + "'";

Sure. There is no other way.
YOu have to use sql query (SELECT x FROM y WHERE a = b) if condition is needed.
All with a sqlCommand help (sqlCommandBuilder is not needed here).

Hey Thanks For your post! I was trying your suggested syntax.while implementing as directed i am not getting any error but its not working also.
Following i the code ,maybe its not comaparing at all :

SqlCommand sc = new SqlCommand(("SELECT * FROM SR1 WHERE DateTime='25/3/2011' "), cs);

                DateTime dat = Convert.ToDateTime(sc);

                DateTime ct = DateTime.Now;
                //if (DateTime.Compare(dat, ct) == 0) 
                if (dat == ct)
                { MessageBox.Show("test"); }
                else { MessageBox.Show("error"); }

I have tried it both by compare method and if else but still its not displaying either one of messages.Please point out mistakes if any.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.