|
-
Jul 23rd, 2008, 04:42 PM
#1
Thread Starter
Member
[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?
-
Jul 23rd, 2008, 04:46 PM
#2
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 
-
Jul 23rd, 2008, 06:29 PM
#3
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.
-
Jul 23rd, 2008, 09:06 PM
#4
Thread Starter
Member
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.
-
Jul 23rd, 2008, 09:29 PM
#5
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:
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.
-
Jul 24th, 2008, 12:51 PM
#6
Thread Starter
Member
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
-
Jul 24th, 2008, 06:51 PM
#7
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.
-
Jul 24th, 2008, 07:24 PM
#8
Thread Starter
Member
Re: Using Date variables for queries
Duly noted mate
Once again thanks for your advice
-
Jul 24th, 2008, 08:05 PM
#9
Re: Using Date variables for queries
 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.
-
Jul 24th, 2008, 08:05 PM
#10
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
-
Jul 27th, 2008, 02:37 PM
#11
Thread Starter
Member
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?
-
Jul 28th, 2008, 11:52 AM
#12
Fanatic Member
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?
-
Jul 28th, 2008, 01:45 PM
#13
Thread Starter
Member
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."
-
Jul 28th, 2008, 02:19 PM
#14
Fanatic Member
Re: Using Date variables for queries
I believe you need to clear your datasource first. Try this
vb Code:
DataGridView.DataSource = null;
DataGridView.Rows.Clear();
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?
-
Jul 28th, 2008, 03:55 PM
#15
Thread Starter
Member
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."
-
Jul 28th, 2008, 07:17 PM
#16
Re: Using Date variables for queries
Clanguage has posted C# code. In VB this line:
CSharp Code:
DataGridView.DataSource = null;
becomes this:
VB.NET Code:
DataGridView.DataSource = Nothing
-
Jul 28th, 2008, 07:38 PM
#17
Thread Starter
Member
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
-
Jul 28th, 2008, 08:05 PM
#18
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?
-
Jul 28th, 2008, 08:43 PM
#19
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|