简体   繁体   中英

Calculating total number of entries between two dates

I have a webform that queries an Access database for journal entries and gives various statistics about them; such as giving me the total number of journal entries and total number of journal entries for a specific topic. What I would like is to be able to enter a start date and an end date and the total number of journal entries that fall between those two dates would be calculated.

So far I have two textboxes with a jquery calendar but when two dates are selected and the search button is clicked, it's not picking up the right number of journal entry totals and returns 0.

The datetime picker puts the date into a format like this: mm/dd/yyyy and the Access database has the JournalDate field formatted as Date/Time. In other words, the Access database holds the dates as 4/8/2014, and the DataTime Picker displays them in the textbox as 04/08/2014. I thought converting them with parse would do the trick but it still returns 0 for the total number of journal entries when dates are selected. Any help would be very much appreciated.

It's also open to SQLInjection right now.

DateTime StartDate = DateTime.Parse(txtStartDate.Text);
DateTime EndDate = DateTime.Parse(txtEndDate.Text);

using (OleDbConnection con = new OleDbConnection(constr))
    using (OleDbCommand com = new OleDbCommand("SELECT * FROM JournalEntries WHERE JournalDate BETWEEN " + txtStartDate.Text + " and " + txtEndDate.Text, con))
    {
        con.Open();
        using (OleDbDataReader myReader = com.ExecuteReader())
        {
            DataTable dt = new DataTable();
            dt.Load(myReader);
            int count = dt.Rows.Count;
            JournalEntryTotal.Text = count.ToString();
        }
    }

Most likely it's a date conversion issue.

I don't think it's generally a good practice to parse date time string fields without explicitly providing a format. It could be for example that your txtStartDate is 04/08/2014 for April 08 2014 and txtEndDate is 05/03/2014 for May 03 2014 and it makes sense to search for records between these dates, but if the default format is dd/MM/yyyy you will get August 04 2014 and March 05 2014 for the start date and end date respectively, and searching for data between those dates yields 0 results.

Similarly, in the sql query you're running you should use a date conversion function instead of relying on a default format. So it should read something like this (I am using an oracle date conversion function but there should be something similar in access):

"SELECT * FROM JournalEntries WHERE JournalDate BETWEEN to_date('" + txtStartDate.Text + "', 'mm/dd/yyyy') and to_date('" + txtEndDate.Text + "', 'mm/dd/yyyy')"

And another word about date fields and formats. Dates don't have formats. A date represents a specific instant in time. August 10 2014 10:23:05 EST is the same date no matter how you write it. When you write it you use a format to display it so that whoever reads it understands exactly what instant in time you are referring to.

I'd first tune the query itself ie in MS SQL maangement studio and then format the date to correct format, ie:

Format(Me.DateTimePicker1.Value, "MM/dd/yyyy")

If you tried, I suspect that your problem is in the upper/lower case of the format string. Note, that month is upper case, while the rest is lower case (important!). Not like what you wrote above: mm/dd/yyyy.

Regards,

Libor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM