Storing values without using Session var.

After the page loads and the 2 strings "rbid" and "rbmt" are validated, I want to "store"
them to be used later and then inserted into the database when Submit is clicked.
(I want these values to persist over any amount of time so using a Session variable won't work)
With VB, I used a Property.

What is the Object Oriented Programming approach (or the best way) in C# to do this?
If you have code examples, that would be great. Thanks

public partial class joinssl : System.Web.UI.Page
  {
 
  protected void Page_Load(object sender, EventArgs e)
    {
    string rbid = Request.QueryString["rbid"];
    string rbmt = Request.QueryString["rbmt"];
    //  need to insert those values into the Database later, but don't want to
    //  rely on using  Request.QueryString[] when the insertion occurs
LVL 16
MikeMCSDAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
Since these are light-weight values, and you only want them to apply to the current page, you could easily use the ViewState to persist the objects.

   if (!Page.IsPostBack)
   {
     ViewState["rbid"] = Request.QueryString["rbid"];
     ViewState["rbmt"] = Request.QueryString["rbmt"];
   }

Bob

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ChumadCommented:
Are you just wanting to make them be variables that are usable throughout the entire class instead of just in the page_load method? if so, do this:

public partial class joinssl : System.Web.UI.Page
  {
  //define some "global-class" variables that can be used anywhere inside this page
  private string rbid = "";
  private string rbmt = "";
 
  protected void Page_Load(object sender, EventArgs e)
    {
    rbid = Request.QueryString["rbid"];
    rbmt = Request.QueryString["rbmt"];
    //  need to insert those values into the Database later, but don't want to
    //  rely on using  Request.QueryString[] when the insertion occurs
}

private void someMethod() {
    //now in here you can use them again as needed
   string blah = rbmt;   //this value was set in the page_load method
}
MikeMCSDAuthor Commented:
thanks Bob . .
what syntax do I use to retrieve the value later in the code, ex:

ViewState["rbid"] = Request.QueryString["rbid"];
...

     CreateProduct(newName.Text,  ViewState["rbid"], . .




OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Bob LearnedCommented:
CreateProduct(newName.Text,  ViewState["rbid"].ToString(),

    or

CreateProduct(newName.Text,  (int)ViewState["rbid"],

depending on the data type stored.

Bob
MikeMCSDAuthor Commented:
Looking back at some VB code, I was actually doing the same thing
with the viewstate using a Property:

   Private Property CustomerID() As Integer
      Get
         Dim value As Integer = 0
         If Not ViewState("CustomerID") Is Nothing Then
            value = DirectCast(ViewState("CustomerID"), Integer)
         End If
         Return value
      End Get
      Set(ByVal value As Integer)
         ViewState("CustomerID") = value
      End Set
   End Property

Is it better to use a Property or does it not matter?
Bob LearnedCommented:
I love the idea of using properties, but only if they need to be public.

Bob
ChumadCommented:
There's nothing wrong with using private properties inside a class... it's all about personal preference:

private int customerID {
   get {
      int retVal = 0;
      if (ViewState["customerID"] != null) {
         retVal = (int)ViewState["customerID"];
      }
      return retVal;
   }
   set { ViewState["customerID"] = value; }
}
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.