Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practice for storing data in an ASP.NET web page

Tags:

c#

asp.net

I have a multi-user set of ASP.NET web pages. The pages use AJAX update panels so I can avoid updating the screen on every postback. The lifecycle of each page is as follows:
1. During Page_Load, get relevant data for user from a web service.
2. Store the data (quite large), and a service reference in a static dataset.
3. allow various edits to parts of the data via the screen controls (grids, text boxes)
4. validate data captured via form
5. send updated data back to service

I am doing this using static variables in the Page class itself as follows:

public partial class MyPage : System.Web.UI.Page
{
    static xxxx.DataCaptureServiceClient  m_Service; //reference to web service
    static string m_PersonID = string.Empty;  //current person_id page is viewing
    static ServResponse m_ServiceResult = null;        // reference to our data to edit ( ServResponse is a large data contract)   
    static string m_SortExpression = "Reference"; //default sort expression for grid

    const int PERSONID_COLUMN = 0;        //column index in grid for the personID column
    const int STATUS_COLUMN = 4;          //column index in grid for the application status

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                // Get new service instance.
                m_Service = new xxxx.DataCaptureServiceClient();

                ShowDataOnPage(); //get data in m_ServiceResult and bind to a grid on screen
            }
        }
        catch (Exception ex)
        {
            Response.Redirect("ErrorPage.aspx", false);
        }
    }

    protected void butNext_Click(object sender, EventArgs e)
    {
        try
        {
            Page.Validate();

            if (Page.IsValid)
            {
                // Use m_ServiceResult and m_Service to send a packaged submission to the service.
                SendDatatoService();
                Response.Redirect("TheNextPage.aspx", false);

            }
        }
        catch (Exception ex)
        {
           Response.Redirect("ErrorPage.aspx", false);
        }
}

//Other methods which allow edits to m_ServiceResult

I am wondering if:

A) This is a good way to implement or are there better practices?
B) Should I clear down memory by setting all statics to NULL when I redirect to another page?
C) If I clear down the statics do I risk another user losing data?

UPDATE

I have rewritten removing the statics, keeping the const values and passing data I need around as parameters. Where I need to keep data for updates I have kept the minimal amount I need in session[] variables.

like image 748
Paul Avatar asked Aug 15 '11 14:08

Paul


People also ask

What database does ASP.NET use?

ASP.NET applications can use all of the popular databases, including Microsoft SQL Server, MySQL, MariaDB, Postgres, MongoDB and CouchDB.

What should you do before using ASP.NET web service?

Before using the web service, a proxy must be created. The proxy is registered with the client application. Then the client application makes the calls to the web service as it were using a local method. The proxy takes the calls, wraps it in proper format and sends it as a SOAP request to the server.

How does ASP.NET maintain state in between subsequent request?

ASP.NET provides many different ways to persist data between user requests. You can use the Application object, cookies, hidden fields, the Session or Cache objects, and lots of other methods. Deciding when to use each of these can sometimes be difficult.


2 Answers

A) No - what happens if a 2nd user opens a page while another one is busy? The static dataset will be overwritten with the 2nd user's data, or is your static dataset somehow differentiating different users' data at the same time?

B) If you absolutely must use statics / server side data, then yes, you should clear them somehow. Guaranteeing that this happens is difficult however. (E.g. if one user just closes their browser)

C) Possibly, but if this is a concern then my question in A) is already going to cause greater problems for you.

As a general answer, storing large amounts of data in memory on the server is generally bad practice. It does not scale well, and opens you up for many different types of errors. Your back-end should be stateless, and there are a number of ways you could achieve that, for example storing the records in a separate table in the DB, which only get finalised (and thus moved into the "real" tables) at the end of the several screens you have.

like image 68
Daniel B Avatar answered Sep 28 '22 19:09

Daniel B


In direct answer to your questions, without opening a can of worms

A) There is pretty much no worse way to implement data capture
B) Setting variables to null in .NET does not clear down memory.
C) Yes, yes you do. By definition every user is sharing the same static data.

like image 31
Jamiec Avatar answered Sep 28 '22 21:09

Jamiec