Note that there are some explanatory texts on larger screens.

plurals
  1. POasp.net problem with null cookie
    text
    copied!<p>I'm having problems writing an If statement because it's asking for the value of a cookie, but it's possible that the cookie could be null which breaks the page.</p> <p>Here's the code:</p> <pre><code>If Request.Cookies("myCookie").Value = "1234" then 'do stuff End If </code></pre> <p>I think i need an elegant way of say "If myCookie is not null and has a value of..."</p> <p>Anyone got any thoughts?</p> <p>EDIT:</p> <p>OK, got it working but since there's a lot of if's and else if's going to be going on, I wonder whether there's a better way to do this...</p> <pre><code>If not Request.Cookies("myCookie") is nothing and Request.Cookies("myCookie").Value = "1234" then 'do this Else If not Request.Cookies("myCookie") is nothing and Request.Cookies("myCookie").Value = "5678" then 'do that Else 'do something else End If </code></pre> <p>OK... thanks for all the replies... not sure which is going to be the best approach but i'll try them all and accept the best as the answer.</p> <p>I am also going to add another level of complexity to the question:</p> <pre><code> If Not Request.Cookies("myCookie") is Nothing Then Select Case Request.Cookies("myCookie").Value Case "EZ12" theNumber = "0800 111 1111" Case "ER34" theNumber = "0800 333 3333" Case "RE32" theNumber = "0800 444 4444" Case Else theNumber = "0800 222 2222" End Select Else Select Case Request.Cookies("myCookie2").Value Case "EZ12" theNumber = "0800 111 1111" Case "ER34" theNumber = "0800 333 3333" Case "RE32" theNumber = "0800 444 4444" Case Else theNumber = "0800 222 2222" End Select End If </code></pre> <p>Basically the cases are always going to be the same but it will select the case from one of 2 places depending on whether myCookie has a value.</p> <p>Since there may be quite a few cases is there anyway I can get away with only listing them once.?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload