-2

I have a function that returns data from my DB, I need to save this data in my cs.file on hold. Any idea how to do that ? this is my function : I need to save m_newId for later use

string m_newId;

public void UserInfo(string id, string fullName)
{
   if (id == "0")
    {
        NewId = _WS.AddUser(fullName); //_WS - id my server
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(NewId);
        m_newId = doc.SelectSingleNode("IDENTITY").InnerText;

    }
    else
    {               
        _WS.UpdateUser(id, fullName);

    }    
}
user2560521
  • 476
  • 2
  • 6
  • 18
  • I'm not quite sure what you ultimately want to pull off, but you should probably be saving it anywhere else other than the c# file. Amoung other things, should you actually successfully write to the c# file and it's not proper code and that cs file ever gets compiled again, it will likely break or give you unintended consequences – Rikon Mar 11 '14 at 17:18

1 Answers1

4

Pick one

ViewState

Viewstate is a hidden fields in an ASP.NET page, contains state of those controls on a page whose "EnableViewstate" property is "true".

You can also explicitly add values in it, on an ASP.NET page like:

Viewstate.Add( "TotalStudents", "87" );

Viewstate should be used when you want to save a value between diferent roundtrips of a single page as viewstate of a page is not accessible by another page.

Because Viewstate renders with the page, it consumes bandwith, so be careful to use it in applications to be run on low bandwith.

Session

Session variables are usually the most commonly used.

When a user visits a site, it's sessions starts and when the user become idle or leave the site, the session ends.

Session variables should be used to save and retrive user specefic information required on multiple pages.

Session variables consumes server memory, so if your may have a huge amount visiters, use session very carefully and instead of put large values in it try to put IDs and references

Cookies

Cookies are some values saved in browsers by the website to retrivbbe and use afterwards.

Usually cookies are used to help dynamic websites to identify visitors and retrieve their saved preferences.

Cookies are also used to facilitate auto login by persisting user id in a cookie save in user's browser.

Because cookies have been saved at client side, they do not create performance issues but may create security issues as they can be hacked from browser.

Cache

Cache is probably the least used state feature of ASP.NET.

Cache is basically a resource specific state persistence feature, means unlike session it stick with resource instead of user, for instance: pages, controls etc.

Cache should be used or frequently used pages, controls, and data structures

Data cache can be used to cache frequently used list of values e.g. list of products

Application

Application variables are shared variables among all users of a web application

Application variables behave like static variables and they are substitute of static variables as static variables are stateless in web applications

Only shared values should be persisted in Application variables, and as soon as they are not in use they should be removed explicitly.


Also


Finally remember the following points on your finger-tips:

  • Viewstate is bandwidth hungry
  • Session variables are memory hungry as per number of users
  • Applications variables are shared
  • Cache is memory hungry as per number of resources
  • Cookies are the least secure
Community
  • 1
  • 1
Michel Ayres
  • 5,891
  • 10
  • 63
  • 97