themis themis - 1 year ago 44 Question

How to avoid exception of request querystring in if parameter is not passed

If the parameter variable is not passed then this code throws an exception of "Conversion from string "" to type 'Date' is not valid."

this is my code.

Public Shared ReadOnly Property Request_projectStartDate() As Date
Dim _value As String = Nothing

If Not HttpContext.Current.Request.QueryString("projectStartDate") Is Nothing Then
_value = HttpContext.Current.Request.QueryString("projectStartDate").ToString()
End If

Return CDate(_value)
End Get
End Property

Answer Source

You can check what @Massimiliano has reported and one more check

If Request.QueryString.HasKeys() Then

   // Check for specified querystrings...
   If Not String.IsNullOrEmpty(Request.QueryString("projectStartDate")) Then
       // Your logic
   End If

End If