1

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
        Get
            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
4
  • sorry i made a mystake the error is "Conversion from string "" to type 'Date' is not valid." because it is nothing Commented May 2, 2012 at 10:02
  • What's your question? _value_ will be Nothing if projectStartDate is not on the query string. Commented May 2, 2012 at 10:03
  • how can avoid this exception? "Conversion from string "" to type 'Date' is not valid." Because there is no parameter being passed and the conversion to CDate of nothing throws the exception. Do i need to add a default date there? like 1900/01/01? or is there a way to return nothing ? Commented May 2, 2012 at 10:06
  • An empty string is not the same as Nothing. Commented May 2, 2012 at 10:27

3 Answers 3

14

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
Sign up to request clarification or add additional context in comments.

3 Comments

the problem is when i convert nothing to cdate actually.
You dont have to convert nothing into CDate these 2 checks will make sure that there is something (PerojectStartDate) which you can type case by using cdate. are you still getting error?
thanx. Correct, i don't need to do cdate. Thanx
1
If Not String.IsNullOrEmpty(Request.QueryString("projectStartDate")) Then
    //
End If

1 Comment

the problem is when i convert nothing to cdate actually.
0

If Request.QueryString("projectStartDate") IsNot Nothing And Request.QueryString("projectStartDate") <> "" Then

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.