Input parameter throwing error while converting

codingrocks

New member
Joined
Apr 9, 2009
Messages
1
Programming Experience
Beginner
I have developed an ASP .NET application in which I am passing some date type input parameters to database.

here is how i pass it

cmdRequests.Parameters.Add("@DateEntered", SqlDbType.DateTime).Direction = ParameterDirection.Input


If (Convert.ToString(txtDateEntered.Text) = String.Empty) Then
cmdRequests.Parameters("@DateEntered").Value = DBNull.Value
Else
cmdRequests.Parameters("@DateEntered").Value = txtDateEntered.Text
End If

But I am getting error saying "String parameter cannot be converted to datetime"

The input parameter type in stored procedure is varchar and i am trying to convert it to varchar


SET @CONDDATEENTERED= ' R.DateEntered = '''+ ''+ (CONVERT(VARCHAR(20),@date,101))
 

rcombs4

Well-known member
Joined
Aug 6, 2008
Messages
189
Programming Experience
3-5
Using the CONVERT funciton with a style of '101' takes your expression and tries to format it to mm/dd/yyyy. I'm assuming in order to do that it must first cast your expression(@Date) as a DateTime.

I would verify that the date entered is a valid date

VB.NET:
If Not IsDate(txtDateEntered.Text) Then
     MsgBox("Please Enter a Valid Date.")
     Exit Sub   
End If

I'm not 100% sure but the IsDate Function is not 100% effective at prefenting the error. Meaning a date of '02/31/2008' will pass the IsDate Function but will then it will generate the error.
 
Top Bottom