System.NullReferenceException was unhandled by user code using Request.From in VS2008

Posted on 2008-11-17
Last Modified: 2013-11-07
While excuting the following code  with value for fn = "Location"; I received a System.NullReferenceException was unhandled by user code error and tbLocation was not null in the hidfilter.Value.

string ff = Utils.GetFilterFieldsCSV(Convert.ToInt32(ddTable.SelectedValue));
            hidfilter.Value = ff;
            string filtertxt = "";
           string [] filter = hidfilter.Value.Split(new Char [] {','});
           string itemsinVS = "";
          for(int i=0;i < filter.Length;i++) {
                     string fn = filter[i].ToString();
      string tbfn = "tb" + fn;
      string fv = Request.Form[tbfn].ToString();
      if (fv != "") {
            filtertxt += " " + fn + " LIKE('%" + fv.Trim().Replace("'", "''").Replace(";", "");
+ "%') AND";
                              itemsinVS += fn + ",";
                              ViewState[fn] = fv.Trim().Replace("'", "''").Replace(";", "");
                        else {
                              // remove existing value
                              ViewState[fn] = "";
Question by:keithwilson1
    LVL 18

    Expert Comment

    It would help if you indicated where the exception was thrown.

    Check the Request.Form collection for "tbLocation".

    Author Comment

    The exception was thrown at
     string fv = Request.Form[tbfn].ToString();
    LVL 18

    Accepted Solution

    string fv = "";
    if (Request.Form[tbfn] != null) {
         fv = Request.Form[tbfn].ToString();

    Author Closing Comment

    The problem was created by AJAX extensions but the Guru worked with me and I finally understood that I had to remove them.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now