Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Error using date is select statement

Author  Topic 

Mondeo
Constraint Violating Yak Guru

287 Posts

Posted - 2007-11-01 : 11:37:21
I've got the code below, it gives me an exception

The conversion of a char data type to a datetime data type resulted in an out of range datetime value.

The error is the same if I used the parameterized query as below or if I concatenate the date into the select statement.

The select statement works fine if I run it directly in query analyser in sql.


Dim db as New SQLDatabase
Dim sql As String = "SELECT vehicleref,manufacturer+' '+model+' '+derivative+' '+ coalesce(additionalfreetext,'') AS Vehicle,"
sql += "expirydate, mustbeorderedby, mustbedeliveredby FROM vwMatrixDOCReport "
sql += " WHERE (convert(datetime,convert(char(10),expirydate,101)) = @sqldate"
sql += " OR convert(datetime,convert(char(10),mustbeorderedby,101)) = @sqldate"
sql += " OR convert(datetime,convert(char(10),mustbedeliveredby,101)) = @sqldate)"
sql += " ORDER BY vehicleref"
Dim params As New Collections.Specialized.StringCollection
params.Add("@sqldate|" + GetSQLDate())
Try
db.openDB()
dtDates = db.GetDataSet(sql, params).Tables(0)
dgDates.DataSource = dtDates
Catch ex As Exception
ExceptionHandling.ShowError(ex, "Could not get the information for the report")
Finally
db.closeDB()
End Try

Private Function GetSQLDate() As String
Return "1900-01-01"
'Return Now.Year.ToString + "-" + Now.Month.ToString + "-" + Now.Day.ToString
End Function

Public Function GetDataSet(ByVal sql As String, ByVal params As Collections.Specialized.StringCollection) As DataSet
Try
Dim myCmd As SqlCommand = New SqlCommand(sql, conn)

For Each s As String In params
Dim p() As String = s.Split("|")
If Not p(1).ToLower = "dbnull.value" Then
myCmd.Parameters.AddWithValue(p(0), p(1))
Else
myCmd.Parameters.AddWithValue(p(0), DBNull.Value)
End If

Next
Dim DA As SqlDataAdapter = New SqlDataAdapter
DA.MissingSchemaAction = MissingSchemaAction.AddWithKey
DA.SelectCommand = myCmd
Dim ds As DataSet = New DataSet
DA.Fill(ds, "table")
Return ds
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End Function


joldham
Wiseass Yak Posting Master

300 Posts

Posted - 2007-11-01 : 12:09:46
Mondeo,

Will this work in place of your GetSQLDate() function?

Dim myDateString As String = Date.Today.ToShortDateString

Jeremy W. Oldham
Go to Top of Page

Mondeo
Constraint Violating Yak Guru

287 Posts

Posted - 2007-11-01 : 13:18:29
Hi,

Its the same.

Cheers
Go to Top of Page
   

- Advertisement -