[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Storing values without using Session var.

Posted on 2007-10-17
7
Medium Priority
?
172 Views
Last Modified: 2011-09-20
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
0
Comment
Question by:MikeMCSD
  • 3
  • 2
  • 2
7 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1600 total points
ID: 20096202
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
 
LVL 8

Expert Comment

by:Chumad
ID: 20096350
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
 
LVL 16

Author Comment

by:MikeMCSD
ID: 20096390
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 20096436
CreateProduct(newName.Text,  ViewState["rbid"].ToString(),

    or

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

depending on the data type stored.

Bob
0
 
LVL 16

Author Comment

by:MikeMCSD
ID: 20096563
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20096568
I love the idea of using properties, but only if they need to be public.

Bob
0
 
LVL 8

Assisted Solution

by:Chumad
Chumad earned 400 total points
ID: 20097868
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

825 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