J
John Kotuby
Hi all,
In VS2008 with VB I am trying to re-write an Update query to a SQL Server
2005 table that was working fine when I was simply assembling a string with
in-line variable values like txtCreateDate where the value may be a valid
date such as '01/01/2007' or a null string.
sql = "UPDATE dbo.user_SearchCriteria Set createDate = ' " & txtCreateDate &
" 'where sequence='8' "
The above sql works just fine.
Now that I have gone to the trouble of doing it the "right" way (even though
archaic to most developers) using command parameters so that I can replace
null strings with SqlDateTime.Null (the createDate field is DateTime
nullable) I get the error:
Failed to convert parameter value from a String to a DateTime.
This occurs whether the @txtCreateDate parameter value is set to
SqlDateTime.Null or DateTime.Parse(txtCreateDate).
Remember that when I was simply using the txtCreateDate variable itself the
update worked without a problem.
So below is part of the code. Can anyone tell me what I might be doing
wrong?
===========================================================
Cmd.Parameters.Add(New SqlParameter("@txtCreateDate", SqlDbType.DateTime,
True))
If Trim(txtCreateDate) = "" Then
Cmd.Parameters("@txtCreateDate").Value = SqlDateTime.Null
Else
Cmd.Parameters("@txtCreateDate").Value = DateTime.Parse(txtCreateDate)
End If
sql = "UPDATE dbo.user_SearchCriteria Set createDate=@txtCreateDate where
sequence='8'
Cmd.ExecuteNonQuery()
=============================================================
Thanks for any help.
In VS2008 with VB I am trying to re-write an Update query to a SQL Server
2005 table that was working fine when I was simply assembling a string with
in-line variable values like txtCreateDate where the value may be a valid
date such as '01/01/2007' or a null string.
sql = "UPDATE dbo.user_SearchCriteria Set createDate = ' " & txtCreateDate &
" 'where sequence='8' "
The above sql works just fine.
Now that I have gone to the trouble of doing it the "right" way (even though
archaic to most developers) using command parameters so that I can replace
null strings with SqlDateTime.Null (the createDate field is DateTime
nullable) I get the error:
Failed to convert parameter value from a String to a DateTime.
This occurs whether the @txtCreateDate parameter value is set to
SqlDateTime.Null or DateTime.Parse(txtCreateDate).
Remember that when I was simply using the txtCreateDate variable itself the
update worked without a problem.
So below is part of the code. Can anyone tell me what I might be doing
wrong?
===========================================================
Cmd.Parameters.Add(New SqlParameter("@txtCreateDate", SqlDbType.DateTime,
True))
If Trim(txtCreateDate) = "" Then
Cmd.Parameters("@txtCreateDate").Value = SqlDateTime.Null
Else
Cmd.Parameters("@txtCreateDate").Value = DateTime.Parse(txtCreateDate)
End If
sql = "UPDATE dbo.user_SearchCriteria Set createDate=@txtCreateDate where
sequence='8'
Cmd.ExecuteNonQuery()
=============================================================
Thanks for any help.