So heres my code:
using (SqlDataReader reader = cmd.ExecuteReader())
if (reader.Read())
SearchingDate = (DateTime)reader[0];

The third line of code is where im stuck. In my app, im trying to compare the current datetime against the value of a members date column value. Unfortunately, the third line of code sets the var SearchingDate to the current datetime. Any suggestions??? Thanks in advance and im giving rep points to every good response/answer!!

Recommended Answers

All 3 Replies

Use code tags, please!

using (SqlDataReader reader = cmd.ExecuteReader())
if (reader.Read())
SearchingDate = (DateTime)reader[0];

If that third line is setting SearchingDateto the current date and time, then that must be the value in the first row and column of your data. The actual current date and time only comes from DateTime.Now (see here)... could you also post the SQL command you're running and a short set of example data that gets you this result?

private void compareDates()
        {
            DateTime searchingDate;
            TimeSpan diff = new TimeSpan(0,15,0);
            TimeSpan timeLeft = new TimeSpan();

            


            using (SqlConnection sqlConn = new SqlConnection("sql connection to database"))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "SELECT date FROM WaynokaLogger WHERE AccountID=" + newString;
                    cmd.Connection = sqlConn;
                    MessageBox.Show(oString);
                    sqlConn.Open();
                    using (SqlDataReader reader = cmd.ExecuteReader())
                        if (reader.Read())
                        {
                            searchingDate = (DateTime)reader[0];
                            timeLeft = DateTime.Now - searchingDate;
                            
                        }
                    

                } // END SECOND USING STATEMENT
            } // END FIRST USING STATEMENT

            if (timeLeft < diff)
            {
                MessageBox.Show("Confront member, card has been used < 15 min. ago");
            }
            
        }

Heres my method.

In my app, im using a dgv to display my sql table. Once a members card is scanned, my app locates and populates my dgv with that members data. I have 6 columns,i will name them in order(left to right)ID, firstname, lastname, membership, accountid, and date. Before all my code is executed, the method above is called to compare the members date column value with the current date and time. I need to grab that members date column value temporarily, before that members date column value is updated to the current datetime, to compare with the DateTime.Now.
Btw, my first column is ID. So obviously its not getting a members date column value.

Hi,
hmm, maybe whats wrong here is your select query, it should be like:

cmd.CommandText = @"SELECT date FROM WaynokaLogger WHERE AccountID = '" + newString + "'";

better would be using parametreized query:

cmd.CommandText = @"SELECT date FROM WaynokaLogger WHERE AccountID = @param1";
cmd.Parameters.Add("@param1", SqlDbType.VarChar, 50).Value = newString; //check if this is a varchar (stirng), if not change

Hope it helps,

BTW: as i told you this code must work.

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.