Hello all! So in my app, I have 5 columns, firstname,lastname,membership,accountid, and date.
Once a members card has been scanned, the app figures out if that member is in my sql table or not. If he/she is, it will update their 'date' column in my dgv. If he/she is not, the app will add them to my sql table and redraw my dgv to reflect my updated sql table.
Does anyone know how to pull a specific datetime value from sql table and compare it with the current datetime in C#? I should also better rephrase that, I need HELP with pulling that specific members datetime column value, not so much comparing. I know how to compare datetime values(i.e. TimeSpan) An important thing to add is in the beginning, when the member scans their card, their accountID # will be in my textbox1. And I assigned txtbox1.text to my var called newString. I need to grab that specific members specific datetime column value if he/she is already in my dgv for comparing to current time. Thanks.

Recommended Answers

All 4 Replies

Try something like this:

string first = "someFirstName";
            string last = "someLastName";
            DateTime CurentDate = new DateTime(2011, 10, 5);
            DateTime SearchingDate = DateTime.MinValue;
            using (SqlConnection sqlConn = new SqlConnection("connString"))
                using (SqlCommand cmd = new SqlCommand())
                    cmd.CommandText = "SELECT date FROM members WHERE name = firstname = @first AND lastname = @last";
                    cmd.Connection = sqlConn;
                    using (SqlDataReader reader = cmd.ExecuteReader())
                        if (reader.Read())
                            SearchingDate = (DateTime)reader[0];
            if (CurentDate.Date == SearchingDate.Date)
                //both dates are equal!!
                //BUT DO NOT FORGET TO Compare DATES only - not full date, which includex hours, minutes, seconds, milliseconds)

Yea i need this to compare date and time.. Can you please comment on what the using statements do?

using keyword is instead of calling Dispose() method.
The same is:

SqlConnection sqlConn = new SqlConnection"(connString");
// rest of code..
// on the end you close  or dispose IDisposable object:

//its EXACTLY thw same as:
using (SqlConnection sqlConn = new SqlConnection("connString"))
    // rest of code..

Calling Close() or dispose() methods is not the same. When you close an object, you can actually then open it again, while you Dispose it, this object get equl to null. So you cannot open it again, unless you instanitate it again (using new keyword).

Hope it helps.

Be a part of the DaniWeb community

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