Link to home
Start Free TrialLog in
Avatar of MikeMCSD
MikeMCSDFlag for United States of America

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Chumad
Chumad

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
}
Avatar of MikeMCSD

ASKER

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




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

    or

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

depending on the data type stored.

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

Bob
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial