0

I'm using a combination of aspx webforms and AJAX calls. I want to store variables privately on the C# end that are passed back and forth using ajax (then eventually save those variables to a SQL database before logout).

I've seen similar questions though I'm still unsure

Here's what some of my javascript file looks like:

$.ajax({
  type: "POST",
  url: "Index.aspx/DoStuff",
  data: JSON.stringify({ 'someVariable': varStr }),
  ...
});

My main structure of Index.aspx.cs is like so:

static SomeCollection g_Collection;

protected void Page_Load(object sender, EventArgs e)
{
  g_Collection = new SomeCollection();
}

[WebMethod]
public static string DoStuff(string someVariable)
{
  g_Collection.Add(someVariable);
}

// At some point ajax also calls another method that saves
// the g_Collection to a database *for that user* (important)
// - based on their GUID

For a single user everything works fine, however, when multiple users login the g_Collection gets saved as a singly used variable (as expected since the g_Collection variable is static).
i.e. if 2 logged in users called the DoStuff() they would be adding to the same g_Collection instance.

Since the method DoStuff() is static I've declared g_Collection as static as well.
How do I create instances of g_Collection instead?
What do I need to declare (inside the Page_Load()?) so that g_Collection is an instance to a logged in user, while still keeping the same functionality in my static [WebMethod] methods?

Community
  • 1
  • 1
Serge P
  • 1,591
  • 8
  • 22
  • 42

1 Answers1

0

I am not sure why you want to use a static member in this scenario.

  • If you want a collection per user, you should probably simply use a Session object.
  • If you want a global collection (1 for all users), an Application object.
Kulas
  • 1
  • Session Approach Fails when we open Application in Different Tab.Both Tab may send different Data. – Anjani Aug 19 '16 at 06:22