Static Variable Inside A Class In Web Application

Jul 28, 2011

I understand the problem of the static variable inside a class in the web application, now I try to create a simple class to deal with SQL Server for example, so I created Static Method inside this class, this static method used in insert, update and delete from database, I wonder if this is safe, and there is no problems will appear ?

View 10 Replies


Similar Messages:

State Management :: Keeping Application Variable Or Static Class For Global String?

Nov 18, 2010

I need to keep a global variable throughout the whole time the user is at my site, but do I use Application or a static class? The string variable should store a region name and my site makes a few changes depending on which region that has been set.

I read that Application was mainly for classic ASP, but I also read that a static class cannot be instantiated at runtime (e.g. when the user has logged in).

So, have I got something wrong here and which solution do I use?

View 2 Replies

Public Static Variable Declared In Public Static Class MyGlobals In A 'Code File' Template?

Jan 27, 2011

I have a question about C Sharp ASP.NET:Is there a difference (in code speed, resources) between:public static variable declared in public static class MyGlobals in a 'Code File' template;and the variable declared in a normal 'Class File' template;I use this variable in 2 different Class Files and also in _Default Page codebehind cs file.In fact in my case I need about 20 global variables of type List<string>.

View 9 Replies

State Management :: Lifespan Of A Property Inside A Static Class In IIS?

Oct 17, 2010

I want to know , how good / bad is it to save configuration values in a static properties?I am creating an application which reads configuration from an XML file and a DataContext file, instead of sending the CustomConfig class into the configuration.xml each time I tought about using an ReadXml() method on application_start at the global.ascx

the ReadXml() method would save the data in static properties of CustomConfig and whereever I would want to read that particler data I would ask CustomConfig.PROERTY_NAME

My question is ? how smart it is ? how long does IIS would save the data in a static member? (if as long as the application exists then I'm ok as long as I add the ReadXml() in the application_start)

View 2 Replies

C# - When Implementing The Singleton Pattern In A Web Application The Static Variable Scope?

Jan 25, 2010

want to make sure I am not assuming something foolish here, when implementing the singleton pattern in an ASP .Net web application the static variable scope is only for the current user session, right? If a second user is accessing the site it is a different memory scope...?

View 4 Replies

State Management :: Difference Between Application Object And Static Variable?

Jul 2, 2010

What is difference between Application object and static variable? Where these are stroed in memory.

View 4 Replies

C# - Creating Instances Of A Class Inside A Static PageMethod Thread Safe?

Dec 14, 2010

I am using jQuery to call PageMethods. For certain operations, the current user credentials must be validated and for other operations, I need to call other static methods. Here is some sample code:

Sample #1

[WebMethod]
public static void PostComment(string comment)
{
UserAuth auth = new UserAuth();
if (auth.isAuthenticated)
{
//Post comment here...
}
}

Sample #2

[WebMethod]
public static string GetComment(int commentId)
{
commentDto comment = //get comment data from the database...
string friendlyDate = ConvertFriendlyDate(comment.commentDate);
return friendlyDate + " " + comment.text;
}
public static string ConvertFriendlyDate(DateTime commentDate)
{
string friendlyDate = //call static utility method to convert date to friendly format
return friendlyDate;
}

Will I be safe using these kinds of operations? Am I better to drop page methods and just call a separate ASPX page for my AJAX requests?

View 4 Replies

C# - How To Lock A Private Static Field Of A Class In One Static Method

Mar 26, 2011

I have a private static field in my Controller class in an MVC web application.

I have a static method in that controller that assigns some value to that static field, I want to apply lock on that static field until some other instance method in the controller uses the value stored in the static field and then releases it.

DETAILS:

I have a controller named BaseController having a static ClientId field as follows and two methods as follows:-

public static string ClientId = "";
static void OnClientConnected(string clientId, ref Dictionary<string, object> list)
{
list.Add("a", "b");
// I want the ClientId to be locked here, so that it can not be accessed by other requests coming to the server and wait for ClientId to be released:-
BaseController.clientId = clientId;
}
public ActionResult Handler()
{
if (something)
{
// use the static ClientId here
}
// Release the ClientId here, so it can now be used by other web requests coming to the server.
return View();
}

View 1 Replies

C# - Declaring Global Variable/class In Application Scope 3 Ways - Which One Is Best

Mar 3, 2010

I am looking for differences between those 3 ways of using static class in asp.net application scope.
Will all of these point to the same class?

Which one is preferable >object< defined declaratively inside global.asax or static class ?

examples:

<object runat="server" scope="application" class="classname" ID="objID"></object>

VS

public static class classname {}

VS

Application("a") = new classname();

View 1 Replies

Public Variable C# - Variable Is Assigned A Value In Another Class?

Mar 7, 2010

[Code]....

The variable is assigned a value in another class like this:

[Code]....

View 1 Replies

C# - Where Does Static Variable Work In Page

Apr 26, 2010

I had an interview today and every thing was going very good, but then an interviewer asked me a question Where Does Static Variable Work in C#- At Application Level or At Page Level.I was not very much clear about this answer as I only knew that static variables are stored on heap and I didn't knew anything about web related thing.

Then he tried to make me more clear by giving an example that in a page I am using static variable and three users are accessing the page one of the user updates the value of static variable, What value will be visible to remaining two users an old copy or the update will be reflected.

View 3 Replies

Web Forms :: How To Reference A Static Variable In Another Folder

Feb 26, 2011

I dont know, but <%= %> doesnt allow me to enter my project namespace. Im trying to reference a static variable in another folder.

Ex: <%= Site.Web.Variable %>

View 1 Replies

State Management :: Cache Vs Static Variable?

Sep 24, 2010

I know the big difference, but one thing is confusing me. Cache works only on a single machine on webfarm and to make it globally we use Velocity or memcache.So is static does same, or is it already work globally ?
eg

Cache["someid"] = "value"; //this will only work on single machine cache,

static string abc = "value"; // will this work on single machine or globally ?

View 6 Replies

Losing Data From Static Property Variable?

Feb 28, 2010

public static int _CompanyId;

I am assigning some value to _CompanyId after login .After some post backs i lose value in_CompanyId .What could be the reason ?

View 4 Replies

Configuration :: Static Variable Being Reset To Null By IIS?

Feb 3, 2011

my asp.net app uses dynamic stylesheets. so each of my pages uses a pre_init method to load the relevant stylesheet. the stylesheet is the same for ALL users of the app.for each page load, i don't want to query the database to determine which stylesheet to use. therefore, I simply load the stylesheet from a STATIC variable that I store when the app starts.

works fine locally. however, on the production server the STATIC variable is reset to Null after about 5 minutes. why is that?? is there a better way for me to handle this? Should I change something in IIS? Should I change the methodolgoy I am using to retreive the stylesheet?

Each .aspx page includes:

[Code]....

BusinessLogic.Admin:

[Code]....

BusinessLogic.Admin parameter

[Code]....

View 4 Replies

Web Forms :: Accessing Static Variable In Global.ashx File?

Sep 28, 2010

I want to access the static variable in my global.ashx file I am using classname.variable name for accessing the variable but the variable name dose not appear.Can someone tell me how to access static varialbe in *.ashx file.

View 5 Replies

State Management :: Creating A Static Variable And Adding To Session?

Oct 29, 2010

I am creating a static variable and adding to session.This variable has value that is user specific. Now I am getting a problem with this users are complaining that they are getting values that should be another users. It's like a mix up with session. Could this be because of static variable declaration?

View 2 Replies

Static Class In App_Code?

Jan 8, 2010

If a static class is put in App_Code, does that imply only one instance will be created, shared by different http requests?Or each request still brings one request?

View 3 Replies

How To HttpContext.Current Work, Providing Context Appropriate State Off A Static Variable

Mar 31, 2010

I've always wondered how you can access the correct state of the current http context via a static method:

HttpContext.Current.Session["foo"] = "bar";

In any other program, anywhere else, my initial assumption about working with a static accessor like this is that changing it will change it across all threads. Similarly, another thread running my change it on me while I am trying to use it.But HttpContext.Current does not behave like this. It provides the appropriate state for the given request, even through the static accessor.

View 3 Replies

Use A Static DbUtil Class For Web Is It Dangerous?

Oct 4, 2010

is it ok to use something like this in the web:
(my application is on asp.net mvc)

public static class DbUtil
{
public static int Insert(object o, string cs)
{
using (var conn = new SqlConnection(cs))
using (var cmd = conn.CreateCommand())
{
conn.Open();
return Convert.ToInt32(cmd.ExecuteScalar());

[Code]....

View 2 Replies

MVC :: Accessing An Existing Static Class?

Mar 15, 2011

So i have some existing functionality from an older ASP.Net app that is a static class.

Say there's a static method in that class that returns a dataset that I would like to bind to something like a gridview or whatever I can build using MVC3.

View 2 Replies

C# - Access Static Class From Form?

May 10, 2010

I am using <%# MyFormatClass(Eval("fieldname")) %> to display data in a grid view on my page. MyFormatClass works fine, but now I want to move it to a central location that can be used by many pages. When I try to access the class <%# Utils.MyFormatClass(Eval("fieldname")) %> it no longer works. Is this something we are allowed to do?

View 1 Replies

Database - Storing Connection In A Static Class?

Mar 4, 2010

Since I'm using Postgresql and can't use LINQ to SQL, I wrote my own wrapper classes.

This is a part of the Student class:

[code]....

It works now! I replaces all Run methods in the Student class with DB.Run

But I want to know if it will work fine with a lot of people online, not me only. I'm not sure how static things work with ASP.NET, maybe it'll eat a lot of memory?..

View 3 Replies

Way To Generate Links Automatically Via A Static Class

Jun 10, 2010

I would like to generate my links automatically via a static class in each of my aspx pages (or in a common BasePage).Currently I use this:

private const string TEMPLATE =
"~/One.aspx";
public static string Link ()
(
string link = String.Format(TEMPLATE);
return link;
)

But the name of my page, One.aspx is hardcoded. Is it possible to generate the path instead of this hardcoded constant TEMPLATE. You should know that I do not instantiate the class before creating the link.

View 1 Replies

C# - How To Create A Static Array Containing Custom Class

Jan 6, 2010

I have a method that returns an array of custom class objects that are created by parsing a text file. At the moment every time I use it I am rereading the file which isn't very efficient.

What I wat to do is create an array containing the objects when the page loads and store them in an array which can then be used later.

[Code]....

I thought I could create the static object using something like:

static Album myAlbums[] = readArray("Albums.txt");

but I am getting the following error:

A field initializer cannot reference the nonstatic field, method, or property 'B2M._Default.readArray(string)'

I am new to C# so this is probably something dumb. (Feel free to poke fun in my general direction if this is the case!)

View 2 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved