date in the sql query

I have an access db with two short date fields and in my vb.net application I need to

display in a dgw the data between two chosed values. Every time I try the "Data

type mismatch in criteria expression" appears.

I use two Datatimepicker formated as Short. In the database the field are also

formated as Short Date. What could be the reason for that?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim con As OleDb.OleDbConnection = New OleDb.OleDbConnection("Provider = Microsoft.Jet.OleDb.4.0;Data Source=C:\Maint.mdb")

Dim cmd As New OleDb.OleDbCommand

Dim cmda As OleDb.OleDbDataAdapter

Dim dset As New DataSet

'Dim cb As OleDbCommandBuilder = New OleDb.OleDbCommandBuilder

Try

Dim query As String

query = "SELECT * FROM Sarcini WHERE DataInitiere between '" + DateTimePicker1.Value() + "' And '" + DateTimePicker2.Value() + "'"

cmda = New OleDbDataAdapter(query, con)

dset.Clear()

' cb.QuotePrefix = "[

' cb.QuoteSuffix = "]"

con.Open()

cmda.Fill(dset, "Sarcini")

Me.DataGridView1.DataSource = dset.Tables("Sarcini")

con.Close()

Catch ex As OleDb.OleDbException

MsgBox(ex.Message)

End Try

End Sub

[2792 byte] By [manasia] at [2007-12-26]
# 1
what happens if you enter the date manually instead of taking the values from the datetimepicker?
ahmedilyas at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 2
Hi try this:

DateTimePicker1.Value.ToShortDateString

or

if you saved it with time: DateTimePicker1.Value.ToShortDateString + " 00:00:00'"

Stefan

Paule_XE at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 3

I believe issue related to the fact that provider does not recognize values as proper dates due to the fact that values where concatenated. Please use parameterized statement and pass dates as parameters. In this case dates will always be handled properly

Here is sample how to use parameters

http://support.microsoft.com/kb/308049/en-us

VMazur at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 4


If you're using dates in a SQL string (Access database) as parameters then they should be enclosed by pound sign characters (not single quotes).

However, I would recommend using Val's suggestion. Use a Command object with parameters instead so you don't have to be concerned about this syntax.

PaulPClementIV at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 5

I think that the problem is related to the date comparation. If in the Access db I change the fileds from shortdate format into text the program is working but the results are incorect. For exemple : I need the all records between 1/11/2006 to 30/11/2006 on the program dgv I see only the record 23/11/2006 and I have a lot of them on 03/11/2006 or 2/11/2006.

I tried with ShorteDate format but I got the same error code. There is something about access db filed format and my DateTimePicker. Onother issue is the fact that DateTimePicker is showing 12/3/2006 and not 12/03/2006.

manasia at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 6


You've figured out what the problem is but you need the correct syntax as I mentioned. Change the column back to a Date data type in your database and also change your SQL statement:

"SELECT * FROM Sarcini WHERE DataInitiere between #" + DateTimePicker1.Value() + "# And #" + DateTimePicker2.Value() + "#"

PaulPClementIV at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 7

Thanks to all! I solved my problem using # characters but still on the DGV the data format is mm/dd/yyyy and not dd/mm/yyyy.

I will dig more to find out how to solve it. Thanks again!

manasia at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 8
If you will use parameterized queries, then you will not have to deal with the short or long dates and # characters. It will work properly regardless your settings. I believe it is a better way instead of using concatenation.
VMazur at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 9

Now the problem was solved only by half meaning that I use the code from below and on the DGW when I search from 2/11/2006 to 30/11/2006 I receive data field

not in the search domain like 29/10/2006. I don't know why and I use use regional option as English (UK) to have the date formated as dd/MM/yyyy.

I think that the sql query is only looking at the dd values and not to the MM. I need some advice to sole this!

Try

dset.Clear()

Dim query As String'query = "SELECT * FROM Sarcini WHERE DataInitiere between '" + DateTimePicker1.Value.ToString("dd/MM/yyyy") + "' And '" + DateTimePicker2.Value.ToString("dd/MM/yyyy") + "'"

query = "SELECT * FROM Sarcini WHERE DataInitiere >= #" + DateTimePicker1.Value() + "# AND DataInitiere <= #" + DateTimePicker2.Value() + "#"

cmda = New OleDbDataAdapter(query, con)

con.Open()

cmda.Fill(dset, "Sarcini")

Me.DataGridView1.DataSource = dset.Tables("Sarcini")

Catch ex As OleDb.OleDbException

MsgBox(ex.Message)

End Try

con.Close()

manasia at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 10

Thread closed. My problem was solved by formating the datatimepicker as "dd-MMM-yyyy".

Thanks to all.

manasia at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 11

I'm having a simular problem. I'm using an access database whit short dates. I get my dates from 2 DateTimePickers.

I use parameters, and I always get the same error. "The datatypes don't match in expression".

I've tried everything. Other formats, using strings in stead of dates, ... . But nothing works. It's realy frustrating. Can somebody help?

MatrixCow at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 12
Parameters should work properly with all the dates. Could you post your code that takes date from the control and passes it to the query?
VMazur at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 13

This peace of code calls a routine in a DataAcces class

Dim lstHotels As List(Of clsHotel) = clsHotelDA.getHotelsMetCriteria(dtpBegin.Value, dtpEinde.Value, sLand, sRegio, sSterren)

this is the routine with the sql-statement and the parameters

Dim sSql As String = "SELECT DISTINCT tblHotel.* " & _

"FROM tblLand INNER JOIN (tblRegio INNER JOIN tblHotel ON tblRegio.regioID = tblHotel.regioID) ON tblLand.landCode = tblRegio.landCode " & _

"WHERE (tblRegio.regioNaam Like ?) AND (tblLand.land Like ?) AND (tblHotel.aantalSterren Like ?) AND ((tblHotel.hotelID) In (SELECT tblKamer.hotelID " & _

"FROM tblKamer " & _

"WHERE (((tblKamer.kamerID) Not In (SELECT tblKamer.kamerID " & _

"FROM tblReservatie INNER JOIN (tblKamer INNER JOIN tblReservatieKamer ON tblKamer.kamerID = tblReservatieKamer.kamerID) ON tblReservatie.reservatieID = tblReservatieKamer.reservatieID " & _

"WHERE (((?) Between tblReservatie.beginDatum And tblReservatie.eindDatum)) OR (((?) Between tblReservatie.beginDatum And tblReservatie.eindDatum)) OR (((tblReservatie.beginDatum)>=?) AND ((tblReservatie.eindDatum)<=?)))))))"

Dim oPar1 As New OleDbParameter

oPar1.Value = sRegio

Dim oPar2 As New OleDbParameter

oPar2.Value = sLand

Dim oPar3 As New OleDbParameter

oPar3.Value = sSterren

Dim oPar4 As New OleDbParameter

oPar4.Value = oBeginPeriode

Dim oPar5 As New OleDbParameter

oPar5.Value = oEindPeriode

Dim oPar6 As New OleDbParameter

oPar6.Value = oBeginPeriode

Dim oPar7 As New OleDbParameter

oPar7.Value = oEindPeriode

Dim oDataTable As DataTable = clsDatabase.GetDT(sSql, oPar1, oPar2, oPar3,oPar4,oPar5,oPar6,oPar7)

MatrixCow at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 14


A couple of things you should do. First, convert the value returned from the DateTimePicker to a Data data type. Second, make certain to specify the Type of the OleDbParameter. It should be OleDbType.Date for a Date.

PaulPClementIV at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...

.NET Development

Site Classified