locked
If httppContext.Current.Request("NameOfThing") Is Nothing NO LONGER WORKS? RRS feed

  • Question

  • User1254222884 posted

    We are in the midst of upgrading our production serverion to IIS 7.5 (still on ASP.Net 3.51) and the following code, which used to work with IIS 6 and 7.0, is now failing with a NullReferenceException was unhandled error:

    If (Not (httpContext.Current.Request("RSTName") Is Nothing)) AndAlso httpContext.Current.Request("RTSName").ToString <> "" Then

    Anyone know how the h*%#@! I'm supposed to tell if the object is instantiated or not if Is Nothing doesn't work? Frown

    TIA

    Wednesday, December 28, 2011 8:51 AM

Answers

  • User1254222884 posted

    Doh!  Must have been a partial compile or something.  The code that refused to work yesterday (always throwing a NullReferenceException, even when testing with Is Nothing) is now working today.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 28, 2011 8:56 AM

All replies

  • User1254222884 posted

    Doh!  Must have been a partial compile or something.  The code that refused to work yesterday (always throwing a NullReferenceException, even when testing with Is Nothing) is now working today.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 28, 2011 8:56 AM
  • User-158764254 posted

    Glad that you got your issue sorted out - but are you sure your code is correct?

    Is it supposed to be RSTName or RTSName ??

    As a sidenote, I try to avoid doing the double (triple?) lookups like that.  Since you're probably checking the value of RSTName before you use it further down in your code, you might have even a third lookup to get that value again.  i'd probably go something like this:

    Dim RSTName As String = HttpContext.Current.Request("RSTName")

    If Not String.IsNullOrEmpty(RSTName) Then

    That way, i do only one lookup to get the value (or the null).

    Thursday, December 29, 2011 6:28 PM
  • User1254222884 posted

    Yeah. Copied the code wrongEmbarassed.

    In IIS 7.X, on our production server, this code ALWAYS returns a NullReferenceException (if the RSTName <thing> doesn't exist):

    Dim RSTName As String = HttpContext.Current.Request("RSTName")
    

    That's why we do the double lookups, first checking for NULL and then for empty.

    FWIW

    Friday, December 30, 2011 9:12 AM
  • User-158764254 posted

    In IIS 7.X, on our production server, this code ALWAYS returns a NullReferenceException (if the RSTName <thing> doesn't exist):

    Dim RSTName As String = HttpContext.Current.Request("RSTName")
    

    That's why we do the double lookups, first checking for NULL and then for empty.

    No - that code would not return a null ref.  If the RSTName <thing>did not exist, then the call to HttpContext.Current.Request("RSTName") would return a null value and the string variable i defined above RSTName would then be null/nothing.

    Friday, December 30, 2011 9:23 AM
  • User1254222884 posted

    I agree, that's the way it SHOULD work. But, in practice, it does not. Frown

    But only on our production servers (IIS 7.5). Our staging and development servers (IIS 6) work as expected. Not sure if it's some sort of setting or ???

    Friday, December 30, 2011 9:26 AM
  • User-158764254 posted

    This code would cause a null ref exception if the value did not exist in the Request

    httpContext.Current.Request("RTSName").ToString

    calling the ToString method requires that whatever is on the left side of the dot be something other than null/nothing.

    if you're setup to allow an implicit cast though, then this would work - even if the object being retrieved from the Request did not exist.

    Dim RSTName As String = HttpContext.Current.Request("RSTName")

    if you're not setup for an implicit cast, you can also do this

    Dim RSTName As String = DirectCast(HttpContext.Current.Request("RSTName"),String)

    Friday, December 30, 2011 9:38 AM
  • User1254222884 posted

    I'll have to try that sometime... THX.

    Friday, December 30, 2011 9:47 AM