I'm developing a Windows Forms app using VS 2008 with a SQL Server 2005 database.
I've got search form where users can search for data by entering one or
two criteria. One of these is a date which corresponds to a date field
in my database. The user clicks on a button, which feeds the criteria
into a stored procedure. The results are displayed in a datagridview
While the functionality works well with the non-date criteria, whenever a
date criteria is used on it's own a cast error is raised ('Conversion
failed when converting datetime from character string.')
I've tried lots of ways of casting the date (formatting using left,
right and mid finctions, using the substring function etc) but still
can't get it to work. Code and SQL are shown below. Does someone know
where I've gone wrong? 'DateAssayRequested' is the date in question.
'set up and initialise connection
conn = New SqlConnection(etc)
comm = New SqlCommand
comm.Connection = conn
comm.CommandText = "SPSearch"
comm.CommandType = CommandType.StoredProcedure
'create input parameters, set types and values
param = comm.CreateParameter
param.ParameterName = "@assayid"
param.Direction = ParameterDirection.Input
param.Value = txtLabNo.Text
View Complete Post