Wednesday, October 15, 2008

Smalldatetime error

If you see this error:
The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value

it means you have bad date to convert from string to smalldatetime data type in database. It usually occurs when you have a web form that submitted some data to save into database and one of the data is a date string.

For some reason, people will type the date wrong such as 1/1/2008 becomes 1/1/2998. But the smalldatetime data type can only hold a date smaller than June 6 2079.

For ASP.NET, you can add this to your customized validation:

Private Sub ValidateStartDate(ByVal sender As Object, ByVal args As ServerValidateEventArgs)
Dim IsValid As Boolean = False
Dim strSalesDate As String
strSalesDate = Clean(Me.txtAdStart.Text)
If IsDate(strSalesDate) Then
IsValid = True
Dim intYear As Integer
intYear = Year(CDate(strSalesDate))
If intYear > 2075 Then
IsValid = False
End If
End If
args.IsValid = IsValid
End Sub

No comments: