Setting a Default Value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brad Baker

    Setting a Default Value

    I have an asp.net/csharp application that requires a particular variable to
    work properly. This variable is usually passed via a query string in the URL
    when the application is first run but under certain circumstances the query
    string may not contain the variable. So I need some way of establishing a
    default value if one isn't set.

    Is there some way I can set a query string on page_load OR is there some way
    I can use a global variable which is accessible throughout my application
    instead? In pseudo code something like:

    public void Page_Load(objec t sender, EventArgs e)
    {
    if (querystring == null)
    {
    global myvariable = "somevalue" ;
    } else {
    global myvariable = querystring;
    }
    }

    protected void dostuff(object sender, EventArgs e)
    {
    if (myvariable == "foo")
    {
    // do stuff
    }
    }

    Hopefully this makes sense - for clarity sake I have tried to simplify my
    situation as much as possible.

    Thanks
    Brad


  • sloan

    #2
    Re: Setting a Default Value

    Put this in a class somewhere.



    public static string SafeSessionGet( Page pageObj, string requestKey,
    string defaultValue)
    {
    if (pageObj.Reques t[requestKey] == null)
    {
    return defaultValue;
    }
    else
    {
    return pageObj.Request[requestKey].ToString ();
    }
    }




    public static string SafeSessionGet( Page pageObj, string requestKey)
    {
    return SafeSessionGet ( pageObj , requestKey , string.Empty ) ;
    }





    You could write a generics version of this in 2.0 if you wanted, but that
    might be overkill.


    private string m_empUUID = string.Empty;
    public void Page_Load(objec t sender, EventArgs e)
    {

    m_empUUID = SafeSessionGet ( Page , "empkey" ) ;
    //or

    m_empUUID = SafeSessionGet ( Page , "empkey" ,
    "00000-00000-000000000-000000000" ) ;


    }






    "Brad Baker" <brad@nospam.no spamwrote in message
    news:ubX6fskmHH A.3952@TK2MSFTN GP03.phx.gbl...
    I have an asp.net/csharp application that requires a particular variable
    to
    work properly. This variable is usually passed via a query string in the
    URL
    when the application is first run but under certain circumstances the
    query
    string may not contain the variable. So I need some way of establishing a
    default value if one isn't set.
    >
    Is there some way I can set a query string on page_load OR is there some
    way
    I can use a global variable which is accessible throughout my application
    instead? In pseudo code something like:
    >
    public void Page_Load(objec t sender, EventArgs e)
    {
    if (querystring == null)
    {
    global myvariable = "somevalue" ;
    } else {
    global myvariable = querystring;
    }
    }
    >
    protected void dostuff(object sender, EventArgs e)
    {
    if (myvariable == "foo")
    {
    // do stuff
    }
    }
    >
    Hopefully this makes sense - for clarity sake I have tried to simplify my
    situation as much as possible.
    >
    Thanks
    Brad
    >
    >

    Comment

    • Brad Baker

      #3
      Re: Setting a Default Value

      I'm a bit of an asp.net/csharp newbie so please bear with me. If I
      understand you correctly, I would call: SafeSessionGet( myvariable) to get
      the query string value (or assign a default value) each time I needed to
      reference myvariable?



      Assuming that's correct, I'm not sure if that's the best solution as I am
      going to assign the default value through a database query. So for instance
      I might have the following (again in pseudo code):



      if myvariable == null {

      run the following SQL query: select * from table where x = 1

      myvariable = first result of sql query

      }



      So for instance if I needed to call myvariable from four different sections
      of my code I would have to run the sql query four times? Isn't that fairly
      inefficient performance wise? I was thinking of trying to set myvariable
      once using one SQL query then reuse it throughout my application. I was
      hoping to do that through some sort of global variable (I'm not sure csharp
      has anything like that or not though).



      Maybe I am misunderstandin g you or maybe there is some other approach I'm
      overlooking. Any additional guidance you could provide would be sincerely
      appreciated. :)



      Thanks Again,

      Brad



      "sloan" <sloan@ipass.ne twrote in message
      news:Ocvto0kmHH A.3952@TK2MSFTN GP03.phx.gbl...
      Put this in a class somewhere.
      >
      >
      >
      public static string SafeSessionGet( Page pageObj, string requestKey,
      string defaultValue)
      {
      if (pageObj.Reques t[requestKey] == null)
      {
      return defaultValue;
      }
      else
      {
      return pageObj.Request[requestKey].ToString ();
      }
      }
      >
      >
      >
      >
      public static string SafeSessionGet( Page pageObj, string requestKey)
      {
      return SafeSessionGet ( pageObj , requestKey , string.Empty ) ;
      }
      >
      >
      >
      >
      >
      You could write a generics version of this in 2.0 if you wanted, but that
      might be overkill.
      >
      >
      private string m_empUUID = string.Empty;
      >
      >public void Page_Load(objec t sender, EventArgs e)
      >{
      >
      >
      m_empUUID = SafeSessionGet ( Page , "empkey" ) ;
      //or
      >
      m_empUUID = SafeSessionGet ( Page , "empkey" ,
      "00000-00000-000000000-000000000" ) ;
      >
      >
      >
      >}
      >
      >
      >
      >
      >
      >
      >
      "Brad Baker" <brad@nospam.no spamwrote in message
      news:ubX6fskmHH A.3952@TK2MSFTN GP03.phx.gbl...
      >I have an asp.net/csharp application that requires a particular variable
      to
      >work properly. This variable is usually passed via a query string in the
      URL
      >when the application is first run but under certain circumstances the
      query
      >string may not contain the variable. So I need some way of establishing a
      >default value if one isn't set.
      >>
      >Is there some way I can set a query string on page_load OR is there some
      way
      >I can use a global variable which is accessible throughout my application
      >instead? In pseudo code something like:
      >>
      >public void Page_Load(objec t sender, EventArgs e)
      >{
      > if (querystring == null)
      > {
      > global myvariable = "somevalue" ;
      > } else {
      > global myvariable = querystring;
      > }
      >}
      >>
      >protected void dostuff(object sender, EventArgs e)
      >{
      > if (myvariable == "foo")
      > {
      > // do stuff
      > }
      >}
      >>
      >Hopefully this makes sense - for clarity sake I have tried to simplify my
      >situation as much as possible.
      >>
      >Thanks
      >Brad
      >>
      >>
      >
      >

      Comment

      • =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=

        #4
        Re: Setting a Default Value

        if your default value needs to come out of the database but it does not change,
        the make the Sql call from within Application_Sta rt in Global.asax. You can
        then either store it in Application state or in a static field in the Global
        class.
        Peter

        --
        Site: http://www.eggheadcafe.com
        UnBlog: http://petesbloggerama.blogspot.com
        Short urls & more: http://ittyurl.net




        "Brad Baker" wrote:
        I'm a bit of an asp.net/csharp newbie so please bear with me. If I
        understand you correctly, I would call: SafeSessionGet( myvariable) to get
        the query string value (or assign a default value) each time I needed to
        reference myvariable?
        >
        >
        >
        Assuming that's correct, I'm not sure if that's the best solution as I am
        going to assign the default value through a database query. So for instance
        I might have the following (again in pseudo code):
        >
        >
        >
        if myvariable == null {
        >
        run the following SQL query: select * from table where x = 1
        >
        myvariable = first result of sql query
        >
        }
        >
        >
        >
        So for instance if I needed to call myvariable from four different sections
        of my code I would have to run the sql query four times? Isn't that fairly
        inefficient performance wise? I was thinking of trying to set myvariable
        once using one SQL query then reuse it throughout my application. I was
        hoping to do that through some sort of global variable (I'm not sure csharp
        has anything like that or not though).
        >
        >
        >
        Maybe I am misunderstandin g you or maybe there is some other approach I'm
        overlooking. Any additional guidance you could provide would be sincerely
        appreciated. :)
        >
        >
        >
        Thanks Again,
        >
        Brad
        >
        >
        >
        "sloan" <sloan@ipass.ne twrote in message
        news:Ocvto0kmHH A.3952@TK2MSFTN GP03.phx.gbl...
        Put this in a class somewhere.



        public static string SafeSessionGet( Page pageObj, string requestKey,
        string defaultValue)
        {
        if (pageObj.Reques t[requestKey] == null)
        {
        return defaultValue;
        }
        else
        {
        return pageObj.Request[requestKey].ToString ();
        }
        }




        public static string SafeSessionGet( Page pageObj, string requestKey)
        {
        return SafeSessionGet ( pageObj , requestKey , string.Empty ) ;
        }





        You could write a generics version of this in 2.0 if you wanted, but that
        might be overkill.


        private string m_empUUID = string.Empty;
        public void Page_Load(objec t sender, EventArgs e)
        {

        m_empUUID = SafeSessionGet ( Page , "empkey" ) ;
        //or

        m_empUUID = SafeSessionGet ( Page , "empkey" ,
        "00000-00000-000000000-000000000" ) ;


        }






        "Brad Baker" <brad@nospam.no spamwrote in message
        news:ubX6fskmHH A.3952@TK2MSFTN GP03.phx.gbl...
        I have an asp.net/csharp application that requires a particular variable
        to
        work properly. This variable is usually passed via a query string in the
        URL
        when the application is first run but under certain circumstances the
        query
        string may not contain the variable. So I need some way of establishing a
        default value if one isn't set.
        >
        Is there some way I can set a query string on page_load OR is there some
        way
        I can use a global variable which is accessible throughout my application
        instead? In pseudo code something like:
        >
        public void Page_Load(objec t sender, EventArgs e)
        {
        if (querystring == null)
        {
        global myvariable = "somevalue" ;
        } else {
        global myvariable = querystring;
        }
        }
        >
        protected void dostuff(object sender, EventArgs e)
        {
        if (myvariable == "foo")
        {
        // do stuff
        }
        }
        >
        Hopefully this makes sense - for clarity sake I have tried to simplify my
        situation as much as possible.
        >
        Thanks
        Brad
        >
        >
        >
        >
        >

        Comment

        • Brad Baker

          #5
          Re: Setting a Default Value

          Hmm what do you mean by "if your default value needs to come out of the
          database but it does not change"?

          Basically my app could be called using the following URL:


          Or it could be called using this URL:


          If the former is used (without a myvariable in the URL) then I want to
          default myvariable to a certain value. But the value I want to default to
          may change depending on the ID in the URL. In other words I could also have
          the following URLs:





          Thanks
          Brad


          "Peter Bromberg [C# MVP]" <pbromberg@yaho o.yabbadabbadoo .comwrote in
          message news:F0D8835C-8353-4FAB-BB00-C9C5F2DA8D45@mi crosoft.com...
          if your default value needs to come out of the database but it does not
          change,
          the make the Sql call from within Application_Sta rt in Global.asax. You
          can
          then either store it in Application state or in a static field in the
          Global
          class.
          Peter
          >
          --
          Site: http://www.eggheadcafe.com
          UnBlog: http://petesbloggerama.blogspot.com
          Short urls & more: http://ittyurl.net
          >
          >
          >
          >
          "Brad Baker" wrote:
          >
          >I'm a bit of an asp.net/csharp newbie so please bear with me. If I
          >understand you correctly, I would call: SafeSessionGet( myvariable) to get
          >the query string value (or assign a default value) each time I needed to
          >reference myvariable?
          >>
          >>
          >>
          >Assuming that's correct, I'm not sure if that's the best solution as I am
          >going to assign the default value through a database query. So for
          >instance
          >I might have the following (again in pseudo code):
          >>
          >>
          >>
          >if myvariable == null {
          >>
          > run the following SQL query: select * from table where x = 1
          >>
          > myvariable = first result of sql query
          >>
          >}
          >>
          >>
          >>
          >So for instance if I needed to call myvariable from four different
          >sections
          >of my code I would have to run the sql query four times? Isn't that
          >fairly
          >inefficient performance wise? I was thinking of trying to set myvariable
          >once using one SQL query then reuse it throughout my application. I was
          >hoping to do that through some sort of global variable (I'm not sure
          >csharp
          >has anything like that or not though).
          >>
          >>
          >>
          >Maybe I am misunderstandin g you or maybe there is some other approach I'm
          >overlooking. Any additional guidance you could provide would be sincerely
          >appreciated. :)
          >>
          >>
          >>
          >Thanks Again,
          >>
          >Brad
          >>
          >>
          >>
          >"sloan" <sloan@ipass.ne twrote in message
          >news:Ocvto0kmH HA.3952@TK2MSFT NGP03.phx.gbl.. .
          Put this in a class somewhere.
          >
          >
          >
          public static string SafeSessionGet( Page pageObj, string requestKey,
          string defaultValue)
          {
          if (pageObj.Reques t[requestKey] == null)
          {
          return defaultValue;
          }
          else
          {
          return pageObj.Request[requestKey].ToString ();
          }
          }
          >
          >
          >
          >
          public static string SafeSessionGet( Page pageObj, string requestKey)
          {
          return SafeSessionGet ( pageObj , requestKey , string.Empty ) ;
          }
          >
          >
          >
          >
          >
          You could write a generics version of this in 2.0 if you wanted, but
          that
          might be overkill.
          >
          >
          private string m_empUUID = string.Empty;
          >
          >public void Page_Load(objec t sender, EventArgs e)
          >{
          >
          >
          m_empUUID = SafeSessionGet ( Page , "empkey" ) ;
          //or
          >
          m_empUUID = SafeSessionGet ( Page , "empkey" ,
          "00000-00000-000000000-000000000" ) ;
          >
          >
          >
          >}
          >
          >
          >
          >
          >
          >
          >
          "Brad Baker" <brad@nospam.no spamwrote in message
          news:ubX6fskmHH A.3952@TK2MSFTN GP03.phx.gbl...
          >I have an asp.net/csharp application that requires a particular
          >variable
          to
          >work properly. This variable is usually passed via a query string in
          >the
          URL
          >when the application is first run but under certain circumstances the
          query
          >string may not contain the variable. So I need some way of
          >establishing a
          >default value if one isn't set.
          >>
          >Is there some way I can set a query string on page_load OR is there
          >some
          way
          >I can use a global variable which is accessible throughout my
          >application
          >instead? In pseudo code something like:
          >>
          >public void Page_Load(objec t sender, EventArgs e)
          >{
          > if (querystring == null)
          > {
          > global myvariable = "somevalue" ;
          > } else {
          > global myvariable = querystring;
          > }
          >}
          >>
          >protected void dostuff(object sender, EventArgs e)
          >{
          > if (myvariable == "foo")
          > {
          > // do stuff
          > }
          >}
          >>
          >Hopefully this makes sense - for clarity sake I have tried to simplify
          >my
          >situation as much as possible.
          >>
          >Thanks
          >Brad
          >>
          >>
          >
          >
          >>
          >>
          >>

          Comment

          • Steven Cheng[MSFT]

            #6
            Re: Setting a Default Value

            Hi Brad,

            I agree with sloan that since QueryString variable doesn't support supply
            default value, it is reasonable to use a class property or method to
            wrapper the querystring value. e.g.

            =============== ======
            class ....
            {
            public String GetQueryStringV alue(string name)
            {
            if(HttpContext. Current.Request .Querystring[name] != null)
            {
            //return it
            }else
            {
            //return default value
            }
            }

            }
            =============== =======

            such as class can be reused in multiple pages. Also, if your concern is
            that the default value will be polled out from backend database and you do
            not want to make multiple database query whenever you need to return the
            default value, I think you can add a further property/method to cache the
            default value. That property/method will always return the default value
            from a private variable or from the ApplicationStat e collection that can
            be initialized at application's startup time or the first time the default
            value is requested. How do you think?

            Sincerely,

            Steven Cheng

            Microsoft MSDN Online Support Lead



            =============== =============== =============== =====

            Get notification to my posts through email? Please refer to
            Learn with interactive lessons and technical documentation, earn professional development hours and certifications, and connect with the community.

            ications.



            Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
            where an initial response from the community or a Microsoft Support
            Engineer within 1 business day is acceptable. Please note that each follow
            up response may take approximately 2 business days as the support
            professional working with you may need further investigation to reach the
            most efficient resolution. The offering is not appropriate for situations
            that require urgent, real-time or phone-based interactions or complex
            project analysis and dump analysis issues. Issues of this nature are best
            handled working with a dedicated Microsoft Support Engineer by contacting
            Microsoft Customer Support Services (CSS) at
            http://msdn.microsoft.com/subscripti...t/default.aspx.

            =============== =============== =============== =====



            This posting is provided "AS IS" with no warranties, and confers no rights.

            Comment

            • Brad Baker

              #7
              Re: Setting a Default Value

              I'm hesitant to use classes just because I'm unfamilarity with them :) That
              seems to be the consensus on how to best acheive this but I sitll wondering
              if there isn't a simplier way. My web "app" is basically one aspx page and
              two user controls not incredibly complex.

              Anyway I have added the following to my default.aspx page:
              <%@ import namespace="MyNa mespace" %>

              Then I have added a class file (under app_code/class.cs) with the following:
              namespace MyNamespace
              {
              public class MyClass
              {
              public String GetQueryStringV alue(string name)
              {
              if (HttpContext.Cu rrent.Request.Q ueryString[name] != null)
              {
              //return it
              return HttpContext.Cur rent.Request.Qu eryString[name].ToString();
              }
              else
              {
              //return default value (hard coded for now)
              return "e62bbc7d623f44 5658e131cba90e8 3982";
              }
              }
              }
              }

              From default.aspx I have the following call:
              <%# Foundation.GetQ ueryStringValue (querystringid) ; %>

              Basically I am just trying to start out very simple and confirm that I can
              get the querystringid and print it out. However when I run this I get the
              following error: CS1026: ) expected.

              I assume I'm doing something wrong but I'm a little lost as to where. :) I'm
              going to do a bit more reading on classes but if you have any suggestions
              I'd welcome feedback.

              Thanks
              Brad



              "Steven Cheng[MSFT]" <stcheng@online .microsoft.comw rote in message
              news:LDTXrP2mHH A.1144@TK2MSFTN GHUB02.phx.gbl. ..
              Hi Brad,
              >
              I agree with sloan that since QueryString variable doesn't support supply
              default value, it is reasonable to use a class property or method to
              wrapper the querystring value. e.g.
              >
              =============== ======
              class ....
              {
              public String GetQueryStringV alue(string name)
              {
              if(HttpContext. Current.Request .Querystring[name] != null)
              {
              //return it
              }else
              {
              //return default value
              }
              }
              >
              }
              =============== =======
              >
              such as class can be reused in multiple pages. Also, if your concern is
              that the default value will be polled out from backend database and you do
              not want to make multiple database query whenever you need to return the
              default value, I think you can add a further property/method to cache the
              default value. That property/method will always return the default value
              from a private variable or from the ApplicationStat e collection that can
              be initialized at application's startup time or the first time the default
              value is requested. How do you think?
              >
              Sincerely,
              >
              Steven Cheng
              >
              Microsoft MSDN Online Support Lead
              >
              >
              >
              =============== =============== =============== =====
              >
              Get notification to my posts through email? Please refer to
              Learn with interactive lessons and technical documentation, earn professional development hours and certifications, and connect with the community.

              ications.
              >
              >
              >
              Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
              where an initial response from the community or a Microsoft Support
              Engineer within 1 business day is acceptable. Please note that each follow
              up response may take approximately 2 business days as the support
              professional working with you may need further investigation to reach the
              most efficient resolution. The offering is not appropriate for situations
              that require urgent, real-time or phone-based interactions or complex
              project analysis and dump analysis issues. Issues of this nature are best
              handled working with a dedicated Microsoft Support Engineer by contacting
              Microsoft Customer Support Services (CSS) at
              http://msdn.microsoft.com/subscripti...t/default.aspx.
              >
              =============== =============== =============== =====
              >
              >
              >
              This posting is provided "AS IS" with no warranties, and confers no
              rights.
              >

              Comment

              • sloan

                #8
                Re: Setting a Default Value


                Sorry dude. We've made it as simple as you can.

                If you're in 1.1, then create a subfolder called "App_Code" in your web
                project.
                Under App_Code, create a class called
                "QueryStringHel per" (.cs) or something like that.

                Add the method I made to the class (or Steven's)

                Call the method.

                Eventually you're going to have to add some kind of logic class to your
                application. So you might as well learn with this simple issue.




                "Brad Baker" <brad@nospam.no spamwrote in message
                news:unzKH85mHH A.4132@TK2MSFTN GP02.phx.gbl...
                I'm hesitant to use classes just because I'm unfamilarity with them :)
                That
                seems to be the consensus on how to best acheive this but I sitll
                wondering
                if there isn't a simplier way. My web "app" is basically one aspx page and
                two user controls not incredibly complex.
                >
                Anyway I have added the following to my default.aspx page:
                <%@ import namespace="MyNa mespace" %>
                >
                Then I have added a class file (under app_code/class.cs) with the
                following:
                namespace MyNamespace
                {
                public class MyClass
                {
                public String GetQueryStringV alue(string name)
                {
                if (HttpContext.Cu rrent.Request.Q ueryString[name] != null)
                {
                //return it
                return HttpContext.Cur rent.Request.Qu eryString[name].ToString();
                }
                else
                {
                //return default value (hard coded for now)
                return "e62bbc7d623f44 5658e131cba90e8 3982";
                }
                }
                }
                }
                >
                From default.aspx I have the following call:
                <%# Foundation.GetQ ueryStringValue (querystringid) ; %>
                >
                Basically I am just trying to start out very simple and confirm that I can
                get the querystringid and print it out. However when I run this I get the
                following error: CS1026: ) expected.
                >
                I assume I'm doing something wrong but I'm a little lost as to where. :)
                I'm
                going to do a bit more reading on classes but if you have any suggestions
                I'd welcome feedback.
                >
                Thanks
                Brad
                >
                >
                >
                "Steven Cheng[MSFT]" <stcheng@online .microsoft.comw rote in message
                news:LDTXrP2mHH A.1144@TK2MSFTN GHUB02.phx.gbl. ..
                Hi Brad,

                I agree with sloan that since QueryString variable doesn't support
                supply
                default value, it is reasonable to use a class property or method to
                wrapper the querystring value. e.g.

                =============== ======
                class ....
                {
                public String GetQueryStringV alue(string name)
                {
                if(HttpContext. Current.Request .Querystring[name] != null)
                {
                //return it
                }else
                {
                //return default value
                }
                }

                }
                =============== =======

                such as class can be reused in multiple pages. Also, if your concern is
                that the default value will be polled out from backend database and you
                do
                not want to make multiple database query whenever you need to return the
                default value, I think you can add a further property/method to cache
                the
                default value. That property/method will always return the default value
                from a private variable or from the ApplicationStat e collection that
                can
                be initialized at application's startup time or the first time the
                default
                value is requested. How do you think?

                Sincerely,

                Steven Cheng

                Microsoft MSDN Online Support Lead



                =============== =============== =============== =====

                Get notification to my posts through email? Please refer to
                http://msdn.microsoft.com/subscripti...ult.aspx#notif
                ications.



                Note: The MSDN Managed Newsgroup support offering is for non-urgent
                issues
                where an initial response from the community or a Microsoft Support
                Engineer within 1 business day is acceptable. Please note that each
                follow
                up response may take approximately 2 business days as the support
                professional working with you may need further investigation to reach
                the
                most efficient resolution. The offering is not appropriate for
                situations
                that require urgent, real-time or phone-based interactions or complex
                project analysis and dump analysis issues. Issues of this nature are
                best
                handled working with a dedicated Microsoft Support Engineer by
                contacting
                Microsoft Customer Support Services (CSS) at
                http://msdn.microsoft.com/subscripti...t/default.aspx.

                =============== =============== =============== =====



                This posting is provided "AS IS" with no warranties, and confers no
                rights.
                >
                >

                Comment

                • Brad Baker

                  #9
                  Re: Setting a Default Value

                  I am using 2.0 and I have a subfolder called App_Code as mentioned in my
                  previous post. I have a class file (its just called class.cs). I've defined
                  a namespace (mynamespace) and a class (myclass) and so forth. I guess my
                  confusion is weather I've implemented the class wrong, called the method
                  wrong or if I've screwed up something else entirely different. :)

                  BTW - I am using "C# Web development with ASP.NET" as a resource along with
                  Google and the asp.net website. Does anyone have any other recommendations
                  for learning the basics and/or reference material. Particularly with
                  regards to class files (but in general would be helpful as well)

                  Thanks,
                  Brad


                  "sloan" <sloan@ipass.ne twrote in message
                  news:eH1TuF7mHH A.1220@TK2MSFTN GP03.phx.gbl...
                  >
                  Sorry dude. We've made it as simple as you can.
                  >
                  If you're in 1.1, then create a subfolder called "App_Code" in your web
                  project.
                  Under App_Code, create a class called
                  "QueryStringHel per" (.cs) or something like that.
                  >
                  Add the method I made to the class (or Steven's)
                  >
                  Call the method.
                  >
                  Eventually you're going to have to add some kind of logic class to your
                  application. So you might as well learn with this simple issue.
                  >
                  >
                  >
                  >
                  "Brad Baker" <brad@nospam.no spamwrote in message
                  news:unzKH85mHH A.4132@TK2MSFTN GP02.phx.gbl...
                  >I'm hesitant to use classes just because I'm unfamilarity with them :)
                  That
                  >seems to be the consensus on how to best acheive this but I sitll
                  wondering
                  >if there isn't a simplier way. My web "app" is basically one aspx page
                  >and
                  >two user controls not incredibly complex.
                  >>
                  >Anyway I have added the following to my default.aspx page:
                  ><%@ import namespace="MyNa mespace" %>
                  >>
                  >Then I have added a class file (under app_code/class.cs) with the
                  following:
                  >namespace MyNamespace
                  >{
                  > public class MyClass
                  > {
                  > public String GetQueryStringV alue(string name)
                  > {
                  > if (HttpContext.Cu rrent.Request.Q ueryString[name] != null)
                  > {
                  > //return it
                  > return HttpContext.Cur rent.Request.Qu eryString[name].ToString();
                  > }
                  > else
                  > {
                  > //return default value (hard coded for now)
                  > return "e62bbc7d623f44 5658e131cba90e8 3982";
                  > }
                  > }
                  > }
                  >}
                  >>
                  >From default.aspx I have the following call:
                  ><%# Foundation.GetQ ueryStringValue (querystringid) ; %>
                  >>
                  >Basically I am just trying to start out very simple and confirm that I
                  >can
                  >get the querystringid and print it out. However when I run this I get the
                  >following error: CS1026: ) expected.
                  >>
                  >I assume I'm doing something wrong but I'm a little lost as to where. :)
                  I'm
                  >going to do a bit more reading on classes but if you have any suggestions
                  >I'd welcome feedback.
                  >>
                  >Thanks
                  >Brad
                  >>
                  >>
                  >>
                  >"Steven Cheng[MSFT]" <stcheng@online .microsoft.comw rote in message
                  >news:LDTXrP2mH HA.1144@TK2MSFT NGHUB02.phx.gbl ...
                  Hi Brad,
                  >
                  I agree with sloan that since QueryString variable doesn't support
                  supply
                  default value, it is reasonable to use a class property or method to
                  wrapper the querystring value. e.g.
                  >
                  =============== ======
                  class ....
                  {
                  public String GetQueryStringV alue(string name)
                  {
                  if(HttpContext. Current.Request .Querystring[name] != null)
                  {
                  //return it
                  }else
                  {
                  //return default value
                  }
                  }
                  >
                  }
                  =============== =======
                  >
                  such as class can be reused in multiple pages. Also, if your concern
                  is
                  that the default value will be polled out from backend database and you
                  do
                  not want to make multiple database query whenever you need to return
                  the
                  default value, I think you can add a further property/method to cache
                  the
                  default value. That property/method will always return the default
                  value
                  from a private variable or from the ApplicationStat e collection that
                  can
                  be initialized at application's startup time or the first time the
                  default
                  value is requested. How do you think?
                  >
                  Sincerely,
                  >
                  Steven Cheng
                  >
                  Microsoft MSDN Online Support Lead
                  >
                  >
                  >
                  =============== =============== =============== =====
                  >
                  Get notification to my posts through email? Please refer to
                  >
                  http://msdn.microsoft.com/subscripti...ult.aspx#notif
                  ications.
                  >
                  >
                  >
                  Note: The MSDN Managed Newsgroup support offering is for non-urgent
                  issues
                  where an initial response from the community or a Microsoft Support
                  Engineer within 1 business day is acceptable. Please note that each
                  follow
                  up response may take approximately 2 business days as the support
                  professional working with you may need further investigation to reach
                  the
                  most efficient resolution. The offering is not appropriate for
                  situations
                  that require urgent, real-time or phone-based interactions or complex
                  project analysis and dump analysis issues. Issues of this nature are
                  best
                  handled working with a dedicated Microsoft Support Engineer by
                  contacting
                  Microsoft Customer Support Services (CSS) at
                  http://msdn.microsoft.com/subscripti...t/default.aspx.
                  >
                  =============== =============== =============== =====
                  >
                  >
                  >
                  This posting is provided "AS IS" with no warranties, and confers no
                  rights.
                  >
                  >>
                  >>
                  >
                  >

                  Comment

                  • sloan

                    #10
                    Re: Setting a Default Value


                    Make the function static.

                    Then call it via

                    string x = MyNamespace.MyC lass.GetQuerySt ringValue( //params here// ) ;

                    Every object in your application has a fully qualfied name. You sometimes
                    have to use the full name.


                    (in the future you can also use a "using MyNamespace" at the top of your
                    code.

                    You need to google "C# namespaces" and read some basic info on it.






                    "Brad Baker" <brad@nospam.no spamwrote in message
                    news:%234q%2358 7mHHA.4624@TK2M SFTNGP04.phx.gb l...
                    I am using 2.0 and I have a subfolder called App_Code as mentioned in my
                    previous post. I have a class file (its just called class.cs). I've
                    defined
                    a namespace (mynamespace) and a class (myclass) and so forth. I guess my
                    confusion is weather I've implemented the class wrong, called the method
                    wrong or if I've screwed up something else entirely different. :)
                    >
                    BTW - I am using "C# Web development with ASP.NET" as a resource along
                    with
                    Google and the asp.net website. Does anyone have any other recommendations
                    for learning the basics and/or reference material. Particularly with
                    regards to class files (but in general would be helpful as well)
                    >
                    Thanks,
                    Brad
                    >
                    >
                    "sloan" <sloan@ipass.ne twrote in message
                    news:eH1TuF7mHH A.1220@TK2MSFTN GP03.phx.gbl...

                    Sorry dude. We've made it as simple as you can.

                    If you're in 1.1, then create a subfolder called "App_Code" in your web
                    project.
                    Under App_Code, create a class called
                    "QueryStringHel per" (.cs) or something like that.

                    Add the method I made to the class (or Steven's)

                    Call the method.

                    Eventually you're going to have to add some kind of logic class to your
                    application. So you might as well learn with this simple issue.




                    "Brad Baker" <brad@nospam.no spamwrote in message
                    news:unzKH85mHH A.4132@TK2MSFTN GP02.phx.gbl...
                    I'm hesitant to use classes just because I'm unfamilarity with them :)
                    That
                    seems to be the consensus on how to best acheive this but I sitll
                    wondering
                    if there isn't a simplier way. My web "app" is basically one aspx page
                    and
                    two user controls not incredibly complex.
                    >
                    Anyway I have added the following to my default.aspx page:
                    <%@ import namespace="MyNa mespace" %>
                    >
                    Then I have added a class file (under app_code/class.cs) with the
                    following:
                    namespace MyNamespace
                    {
                    public class MyClass
                    {
                    public String GetQueryStringV alue(string name)
                    {
                    if (HttpContext.Cu rrent.Request.Q ueryString[name] != null)
                    {
                    //return it
                    return HttpContext.Cur rent.Request.Qu eryString[name].ToString();
                    }
                    else
                    {
                    //return default value (hard coded for now)
                    return "e62bbc7d623f44 5658e131cba90e8 3982";
                    }
                    }
                    }
                    }
                    >
                    From default.aspx I have the following call:
                    <%# Foundation.GetQ ueryStringValue (querystringid) ; %>
                    >
                    Basically I am just trying to start out very simple and confirm that I
                    can
                    get the querystringid and print it out. However when I run this I get
                    the
                    following error: CS1026: ) expected.
                    >
                    I assume I'm doing something wrong but I'm a little lost as to where.
                    :)
                    I'm
                    going to do a bit more reading on classes but if you have any
                    suggestions
                    I'd welcome feedback.
                    >
                    Thanks
                    Brad
                    >
                    >
                    >
                    "Steven Cheng[MSFT]" <stcheng@online .microsoft.comw rote in message
                    news:LDTXrP2mHH A.1144@TK2MSFTN GHUB02.phx.gbl. ..
                    Hi Brad,

                    I agree with sloan that since QueryString variable doesn't support
                    supply
                    default value, it is reasonable to use a class property or method to
                    wrapper the querystring value. e.g.

                    =============== ======
                    class ....
                    {
                    public String GetQueryStringV alue(string name)
                    {
                    if(HttpContext. Current.Request .Querystring[name] != null)
                    {
                    //return it
                    }else
                    {
                    //return default value
                    }
                    }

                    }
                    =============== =======

                    such as class can be reused in multiple pages. Also, if your concern
                    is
                    that the default value will be polled out from backend database and
                    you
                    do
                    not want to make multiple database query whenever you need to return
                    the
                    default value, I think you can add a further property/method to cache
                    the
                    default value. That property/method will always return the default
                    value
                    from a private variable or from the ApplicationStat e collection that
                    can
                    be initialized at application's startup time or the first time the
                    default
                    value is requested. How do you think?

                    Sincerely,

                    Steven Cheng

                    Microsoft MSDN Online Support Lead



                    =============== =============== =============== =====

                    Get notification to my posts through email? Please refer to
                    http://msdn.microsoft.com/subscripti...ult.aspx#notif
                    ications.



                    Note: The MSDN Managed Newsgroup support offering is for non-urgent
                    issues
                    where an initial response from the community or a Microsoft Support
                    Engineer within 1 business day is acceptable. Please note that each
                    follow
                    up response may take approximately 2 business days as the support
                    professional working with you may need further investigation to reach
                    the
                    most efficient resolution. The offering is not appropriate for
                    situations
                    that require urgent, real-time or phone-based interactions or complex
                    project analysis and dump analysis issues. Issues of this nature are
                    best
                    handled working with a dedicated Microsoft Support Engineer by
                    contacting
                    Microsoft Customer Support Services (CSS) at
                    http://msdn.microsoft.com/subscripti...t/default.aspx.

                    =============== =============== =============== =====



                    This posting is provided "AS IS" with no warranties, and confers no
                    rights.

                    >
                    >
                    >
                    >

                    Comment

                    • Steven Cheng[MSFT]

                      #11
                      Re: Setting a Default Value

                      Hi Brad,

                      As for the code you posted:

                      ===============
                      Then I have added a class file (under app_code/class.cs) with the following:
                      namespace MyNamespace
                      {
                      public class MyClass
                      {
                      public String GetQueryStringV alue(string name)
                      {
                      if (HttpContext.Cu rrent.Request.Q ueryString[name] != null)
                      {
                      //return it
                      return HttpContext.Cur rent.Request.Qu eryString[name].ToString();
                      }
                      else
                      {
                      //return default value (hard coded for now)
                      return "e62bbc7d623f44 5658e131cba90e8 3982";
                      }
                      }
                      }
                      }

                      From default.aspx I have the following call:
                      <%# Foundation.GetQ ueryStringValue (querystringid) ; %>
                      =============

                      what is "Foundation "? I haven't seen the definitio of it, you can declare
                      the method "GetQueryString Value" as static funciton so as to use it through
                      "Namespace.Clas sName.MethodNam e" as sloan has suggested.

                      Also, in databinding expression(<%# %>) , you do not need to add ";" ,
                      this will also cause error.

                      Hope this helps.

                      Sincerely,

                      Steven Cheng

                      Microsoft MSDN Online Support Lead


                      This posting is provided "AS IS" with no warranties, and confers no rights.

                      Comment

                      Working...