locked
Check for empty cookie RRS feed

  • Question

  • User1510859543 posted

    I am getting the following exception errors in some asp.net pages when converting value from a cookie.

    "Input string was not in a correct format."

    Below is a sample of code in Page_Load that is giving this error.

                Dim intPatientID As Int32 = 0
                If Request.Cookies("PatientID") Is Nothing Then
                    'no PatientID cookie so see if valid logged in user
                    If Not Request.Cookies("OrgID") Is Nothing Then
                        Response.Redirect("~/Ketohome.aspx?msg=Please select a Client first.")
                    Else
                        'not logged in user
                        Response.Redirect("~/KetoStart.aspx")
                    End If
                Else
                    intPatientID = Convert.ToInt32(Request.Cookies("PatientID").Value)
                End If
    

    The error happens in the next to last line above when I try to convert the value in a cookie.  Not sure if the cookie is empty that may be causing this but would like someone's input on how to best handle.  Thanks.

    Wednesday, November 5, 2014 10:00 AM

Answers

  • User724169276 posted
    If String.IsNullOrEmpty(Request.Cookies("PatientID").Value) Then 
             //"is null or empty" 
          Else 
             If Int32.TryParse(Request.Cookies("PatientID").Value, intResult) = True Then
               txtDietType.Text = ClientsClass.DietType(Request.Cookies("PatientID").Value)
             End If
          End If 

    I hope now you will get the output

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 6, 2014 9:14 AM

All replies

  • User-484054684 posted

    If you pass the non-number data to Convert.ToInt32, we have chances to get this exception.

    So, you can use Int32.TryParse method as in this MSDN article: http://msdn.microsoft.com/en-us/library/f02979c7.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

    Wednesday, November 5, 2014 10:19 AM
  • User1176121428 posted

    Hi  dlchase,

    Thanks for your post.

    I suggest that you can try check the cookies if it is null,like this:

    If Request.Cookies("PatientID") IsNot Nothing Then
     //
    End If

    Hope this can be helpful.

    Best Regards,

    Eileen

    Thursday, November 6, 2014 3:27 AM
  • User1510859543 posted

    Will that work if the cookie value = "" (empty string)?

    Why is that different from below which I am using?

    If Request.Cookies("PatientID") Is Nothing Then

    End If

    Thursday, November 6, 2014 8:14 AM
  • User724169276 posted

    Will that work if the cookie value = "" (empty string)?

    Why is that different from below which I am using?

    If Request.Cookies("PatientID") Is Nothing Then

    End If

    Before converting the patientid cookie, can you check whether it is having a valid integer number or not ?

    intPatientID = Convert.ToInt32(Request.Cookies("PatientID").Value)

    Thursday, November 6, 2014 8:45 AM
  • User1510859543 posted

    I am experimenting with something like below.

                    Dim intResult As Integer
                    If Int32.TryParse(Request.Cookies("PatientID").Value, intResult) = True Then
                        txtDietType.Text = ClientsClass.DietType(Request.Cookies("PatientID").Value)
                    End If
    

    Thursday, November 6, 2014 8:54 AM
  • User-484054684 posted

    Hi

    As mentioned above, you get the Format exception, when the integer (number) is not passed to convert. 

    So you can use Int32.TryParse as suggested above. 

    http://msdn.microsoft.com/en-us/library/f02979c7.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1 

    You can handle the logic based on true or false returned by above method. If it is true, it is valid integer, if it is false then there is some error with the value passed in.

    Thursday, November 6, 2014 8:55 AM
  • User724169276 posted

    I am experimenting with something like below.

                    Dim intResult As Integer
                    If Int32.TryParse(Request.Cookies("PatientID").Value, intResult) = True Then
                        txtDietType.Text = ClientsClass.DietType(Request.Cookies("PatientID").Value)
                    End If

    I am not sure about clientclass.diettype but is it going nside the if block ?

    Thursday, November 6, 2014 8:57 AM
  • User1510859543 posted

    This is an example of what I was doing on another page.  Our web app is setting the cookie value and can only be an integer as it comes from an identity column in an SQL table.  I was getting an exception error as mentioned previously and wanted to handle all conditions.  There is no place for the user to enter this value but I am guessing that the cookie becomes expired and maybe no longer there.

    Thursday, November 6, 2014 9:02 AM
  • User-484054684 posted

    You may check what is this value "Request.Cookies("PatientID").Value" appearing when debugging. That should give some clue if you are getting cookie value or not.

    Also as Ashim has suggested, you may also need to cross check the other method you are invoking.

    Thursday, November 6, 2014 9:06 AM
  • User724169276 posted
    If String.IsNullOrEmpty(Request.Cookies("PatientID").Value) Then 
             //"is null or empty" 
          Else 
             If Int32.TryParse(Request.Cookies("PatientID").Value, intResult) = True Then
               txtDietType.Text = ClientsClass.DietType(Request.Cookies("PatientID").Value)
             End If
          End If 

    I hope now you will get the output

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 6, 2014 9:14 AM