Results 1 to 19 of 19

Thread: [RESOLVED] Using Date variables for queries

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    61

    Resolved [RESOLVED] Using Date variables for queries

    Hi all

    I am having a bit of trouble trying to restrict table data using a date . I am using Microsoft Access, an OleDbConenction and OleDBCommand along with a Datagridview. My code is as follows

    Code:
       Dim someDate As Date = #5/18/2008#
            MsgBox(CStr(someDate))
            OleDbCommand1.CommandText = "SELECT * FROM AccessLog WHERE Session_Start > '" + someDate + "'"
            Dim reader As OleDbDataReader = OleDbCommand1.ExecuteReader
            Dim table As New DataTable
            table.Load(reader)
            reader.Close()
            DataGridView1.DataSource = table
            DataGridView1.ReadOnly = True
    any ideas on why I am getting an error?

  2. #2
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: Using Date variables for queries

    what error message and at what line?

    also make sure you are opening connection object as icant see this in code.
    __________________
    Rate the posts that helped you

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Using Date variables for queries

    I think I might put this in a PM and just send it to every member because it seems like I've had to say it to every member anyway:

    Do NOT use string concatenation to build SQL statements. Use parameters to insert variable values into SQL code.

    Follow the Data Access link in my signature to see examples of using parameters with SQL code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    61

    Re: Using Date variables for queries

    Thank you for the advice jmcilhinney, hovever I'm still having problems.

    When I try to restrict values by a whole number everything is fine. However when I try to restrict values by date nothing comes up (the datagridview remains blank)

    my code is as follows


    Code:
            OleDbCommand1.CommandText = "SELECT * FROM AccessLog WHERE Session_Start = @Session_Start"
            OleDbCommand1.Parameters.AddWithValue("@Session_Start", 18 / 5 / 2008)
    
            Dim reader As OleDbDataReader = OleDbCommand1.ExecuteReader
            Dim table As New DataTable
            table.Load(reader)
            reader.Close()
            DataGridView1.DataSource = table
            DataGridView1.ReadOnly = True
    Last edited by micki_free; Jul 23rd, 2008 at 09:19 PM.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Using Date variables for queries

    But you aren't using a date. That's not a date. That is an expression that is evaluated as 18 divided by 5 divided by 2008. This is a date:
    vb.net Code:
    1. OleDbCommand1.Parameters.AddWithValue("@Session_Start", #5/18/ 2008#)
    Now, that said, if you're just going to use a literal value then there's no point using a parameter at all. You may as well just hard code the value right into your SQL code. I assume that this is just for testing though, and you'll be using a variable value for the real thing, like the Value from a DateTimePicker or the like.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    61

    Re: Using Date variables for queries

    Thanks once again jmcilhinney. You're right it was just for testing but it's working fine now.

    There's only one problem I' still having. After I've ran one search and I attempt to run another one (by pressing the button containing the code) the DataGridView just stays as it was. Is there any way to refresh it?

    My code is below

    Code:
            Try
                OleDbCommand1.CommandText = "SELECT * FROM AccessLog WHERE Session_Start > @Session_Start"
                OleDbCommand1.Parameters.AddWithValue("@Session_Start", DateValue(txtStartDate.Text))
    
                Dim reader As OleDbDataReader = OleDbCommand1.ExecuteReader
                Dim table As New DataTable
                table.Load(reader)
                reader.Close()
                DataGridView1.DataSource = table
                DataGridView1.ReadOnly = True
            Catch badcast As InvalidCastException
                MessageBox.Show(badcast.Message)
            End Try

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Using Date variables for queries

    First up, there should be no reason for you that have to catch an InvalidCastException there. If there's a particular reason that you want to use a TextBox instead of a DateTimePicker, which you should be using, then you can at least use Date.TryParse to convert the String to a Date without throwing an exception if it doesn't work.

    As for your question, perhaps try setting the DataSource to Nothing before assigning the DataTable.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    61

    Re: Using Date variables for queries

    Duly noted mate

    Once again thanks for your advice

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Using Date variables for queries

    Quote Originally Posted by jmcilhinney
    As for your question, perhaps try setting the DataSource to Nothing before assigning the DataTable.
    Just note that I'm not sure whether this will work or not. At a glance it looks like your existing code should work. As always, post back if it doesn't do the job.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Using Date variables for queries

    set the datasource to nothing, and see if it also has a .Clear method... if it does, call it after setting the datasource to nothing, but before you attach another one. If it does not have a clear method.... disregard.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    61

    Re: Using Date variables for queries

    Hi guys. I tried setting the Datasource property of the Datagridview to nothing but I am still only able to run one search at a time by pressing the button

    I can not find a clear method for the Datagridview. My code is bleow

    Code:
            OleDbCommand1.CommandText = "SELECT * FROM AccessLog WHERE Session_Start > @Session_Start"
            OleDbCommand1.Parameters.AddWithValue("@Session_Start", DateTimePickerStart.Value.Date)
    
    
            Dim reader As OleDbDataReader = OleDbCommand1.ExecuteReader
            Dim table As New DataTable
            table.Load(reader)
            reader.Close()
    
            DataGridView1.DataSource = ""
    
            DataGridView1.DataSource = table
            DataGridView1.ReadOnly = True

    Any ideas?

  12. #12
    Fanatic Member Clanguage's Avatar
    Join Date
    Jan 2008
    Location
    North Carolina
    Posts
    659

    Re: Using Date variables for queries

    Use the clear method on the rows

    Code:
            DataGridView1.Rows.Clear()
    CLanguage;
    IF Post = HelpFull Then
    RateMe
    Else
    Say("Shut UP")
    End If
    DotNet rocks
    VB 6, VB.Net 2003, 2005, 2008, 2010, SQL 2005, WM 5.0,ahem ?OpenRoad?

  13. #13

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    61

    Re: Using Date variables for queries

    I tried using the following code

    Code:
            OleDbCommand1.CommandText = "SELECT * FROM AccessLog WHERE Session_Start > @Session_Start"
            OleDbCommand1.Parameters.AddWithValue("@Session_Start", DateTimePickerStart.Value.Date)
    
            Dim reader As OleDbDataReader = OleDbCommand1.ExecuteReader
            Dim table As New DataTable
            table.Load(reader)
            reader.Close()
    
            DataGridView1.DataSource = ""
            DataGridView1.Rows.Clear()
    
            DataGridView1.DataSource = table
            DataGridView1.ReadOnly = True
    and I wasn't even able to run the search once. I kept gettin an error saying

    "Rows collection cannot be programmatically cleared when the DataGridView control is data-bound to anything else than an IBindingList that supports change notification and allows deletion."

  14. #14
    Fanatic Member Clanguage's Avatar
    Join Date
    Jan 2008
    Location
    North Carolina
    Posts
    659

    Re: Using Date variables for queries

    I believe you need to clear your datasource first. Try this
    vb Code:
    1. DataGridView.DataSource = null;
    2. DataGridView.Rows.Clear();
    3. DataGridView.Refresh();
    CLanguage;
    IF Post = HelpFull Then
    RateMe
    Else
    Say("Shut UP")
    End If
    DotNet rocks
    VB 6, VB.Net 2003, 2005, 2008, 2010, SQL 2005, WM 5.0,ahem ?OpenRoad?

  15. #15

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    61

    Re: Using Date variables for queries

    I noticed that you assigned the datasource to null. However When I try that I get an error message in the error list

    "'null' is not declared. 'Null' constant is no longer supported; use 'System.DBNull' instead."

    When I try System.DBNull I get the message

    "'DBNull' is a type in 'System' and cannot be used as an expression."

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Using Date variables for queries

    Clanguage has posted C# code. In VB this line:
    CSharp Code:
    1. DataGridView.DataSource = null;
    becomes this:
    VB.NET Code:
    1. DataGridView.DataSource = Nothing
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  17. #17

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    61

    Re: Using Date variables for queries

    Still no joy I'm afraid guys. The Weird thing is that when I click on the button to run the search the Datagriview will flash (as if it is refreshing) yet the data remains the same

    my code is as follows

    Code:
    OleDbCommand1.CommandText = "SELECT * FROM AccessLog WHERE Session_Start > @Session_Start"
            OleDbCommand1.Parameters.AddWithValue("@Session_Start", DateTimePickerStart.Value.Date)
    
            Dim reader As OleDbDataReader = OleDbCommand1.ExecuteReader
            Dim table As New DataTable
            table.Load(reader)
            reader.Close()
    
            DataGridView1.DataSource = Nothing
            DataGridView1.Rows.Clear()
            DataGridView1.Refresh()
    
            DataGridView1.DataSource = table
            DataGridView1.ReadOnly = True
    Any further help you could give me would be much appreciated

  18. #18
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Using Date variables for queries

    Have you actually looked through the data in code to make sure you aren't simply getting the same data over and over?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  19. #19

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    61

    Re: Using Date variables for queries

    OK guys, I found out WHERE the problem lies. Now I just need to work out WHAT the problem is

    I tried the following code after populating the Datatable/DataGridView. It populates a listbox with the data.

    Code:
            'making sure the output is correct
            lstDBTest.Items.Clear()
            Dim reader2 As OleDbDataReader = OleDbCommand1.ExecuteReader
            While reader2.Read()
                lstDBTest.Items.Add(CStr(reader2.Item("Session_No")))
                lstDBTest.Items.Add(CStr(reader2.Item("Person_ID")))
                lstDBTest.Items.Add(CStr(reader2.Item("Session_Start")))
                lstDBTest.Items.Add("")
            End While
            reader2.Close()
            'making sure the output is correct

    Surprisingly the exact same thing happened with the list box (the listbox refreshed but the data stayed exactly the same). So as far as I can tell the problem lies in this block of code here

    Code:
            OleDbCommand1.CommandText = "SELECT * FROM AccessLog WHERE Session_Start > @Session_Start"
            OleDbCommand1.Parameters.AddWithValue("@Session_Start", DateTimePickerStart.Value.Date)
    
            Dim reader As OleDbDataReader = OleDbCommand1.ExecuteReader
    It looks as if either the SQL query is incorrect, the executereader command only executes the first time or the reader does not change as the SQL query changes. Can you guys see anything out of the ordinary in the code?
    Last edited by micki_free; Jul 29th, 2008 at 03:42 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width