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?
 
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
0
 
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
}
0
 
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"], . .




0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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

    or

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

depending on the data type stored.

Bob
0
 
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?
0
 
Bob LearnedCommented:
I love the idea of using properties, but only if they need to be public.

Bob
0
 
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; }
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.