Wednesday, October 13, 2004

SQL Parameter Possibly Null

I needed to include a date column in my datagrid which could possible be null. I therefore had to insert null, or the value entered by the user if appropriate. I wrote a small function which created the parameter:

Function CreateSQLDateParam(ByRef Name As String, ByRef Val As String) As SqlParameter
Dim SQL_P As SqlParameter
SQL_P = New SqlParameter(Name, SqlDbType.DateTime)
If Val <> "" Then
SQL_P.Value = CDate(Val)
Else
SQL_P.Value = DBNull.Value
End If
Return SQL_P
End Function

No comments: