Static property of class
-
Hi All I have a web site. This web site connected multiple databases. After user(1st user) successfully logged in then we received database name, user id, and password and create a connection string and store the value in class file as a static property. Its work all over the website and class file. But my problem is that when another user (2nd User) login then class property is changed with (2nd User) connection string. An user(1st user) data changed with 2nd User data. That means 1st user going connect with 2nd User database. There I any way to recover from this problem, I don’t use different way. Because my project almost completed. Please help me out Thanks Rajesh
-
Hi All I have a web site. This web site connected multiple databases. After user(1st user) successfully logged in then we received database name, user id, and password and create a connection string and store the value in class file as a static property. Its work all over the website and class file. But my problem is that when another user (2nd User) login then class property is changed with (2nd User) connection string. An user(1st user) data changed with 2nd User data. That means 1st user going connect with 2nd User database. There I any way to recover from this problem, I don’t use different way. Because my project almost completed. Please help me out Thanks Rajesh
you can keep the properties in session variables. Vishal http://openreferals.com/~vishalg.aspx[^]
-
Hi All I have a web site. This web site connected multiple databases. After user(1st user) successfully logged in then we received database name, user id, and password and create a connection string and store the value in class file as a static property. Its work all over the website and class file. But my problem is that when another user (2nd User) login then class property is changed with (2nd User) connection string. An user(1st user) data changed with 2nd User data. That means 1st user going connect with 2nd User database. There I any way to recover from this problem, I don’t use different way. Because my project almost completed. Please help me out Thanks Rajesh
As I'm sure you are well aware, static properties values are shared (hence I believe the VB keyword for it) among all the application's instances. Which means your application is seriously flawed if you expose user data to other users. The answer: don't use static properties the way you do. Use normal properties. And don't mean to be rude, but it seems your project is far away from completion, if you want it to work correctly.
var question = (_2b || !(_2b));
-
you can keep the properties in session variables. Vishal http://openreferals.com/~vishalg.aspx[^]
-
Hi All I have a web site. This web site connected multiple databases. After user(1st user) successfully logged in then we received database name, user id, and password and create a connection string and store the value in class file as a static property. Its work all over the website and class file. But my problem is that when another user (2nd User) login then class property is changed with (2nd User) connection string. An user(1st user) data changed with 2nd User data. That means 1st user going connect with 2nd User database. There I any way to recover from this problem, I don’t use different way. Because my project almost completed. Please help me out Thanks Rajesh
I agree with Greg. You are using
static
keyword without knowing how it works. Static variables will stay in memory until the application domain unloads and it is shared between all threads under that application domain. To solve this, Usesession
variables. These are unique for each visitor. :)Navaneeth How to use google | Ask smart questions
-
I agree with Greg. You are using
static
keyword without knowing how it works. Static variables will stay in memory until the application domain unloads and it is shared between all threads under that application domain. To solve this, Usesession
variables. These are unique for each visitor. :)Navaneeth How to use google | Ask smart questions
But how can I used session variable in class file, its through "Objects reference not set to be instant of an objects". Please check my class file. using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.SessionState; namespace MyStore { public class GetConnection { static string _conString = null; public GetConnection() { // // TODO: Add constructor logic here // } public static String DBConnectionString { get { if (HttpContext.Current.Session["ConnStr"] != null) { return HttpContext.Current.Session["ConnStr"].ToString(); } else { return ""; } } set { HttpContext.Current.Session["ConnStr"] = value; } } } }
-
But how can I used session variable in class file, its through "Objects reference not set to be instant of an objects". Please check my class file. using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.SessionState; namespace MyStore { public class GetConnection { static string _conString = null; public GetConnection() { // // TODO: Add constructor logic here // } public static String DBConnectionString { get { if (HttpContext.Current.Session["ConnStr"] != null) { return HttpContext.Current.Session["ConnStr"].ToString(); } else { return ""; } } set { HttpContext.Current.Session["ConnStr"] = value; } } } }
The way you do it is overkill, and it's flawed. You should pass the connection string to the constructor, get rid of the static keyword for the DBConnectionString property, add a private field _dbConnection and assign the value of it to the parameter you get in the constructor.
public class GetConnection
{public GetConnection(string connectionStr)
{
_dbConnection = connectionStr;
}private string _dbConnection;
public string DBConnectionString
{
get{return _dbConnection;}
}}
}It's still overkill. Having a class with one string property that you set to something you already know. As you have the connection string (hopefully stored in web.config), just fetch it from web.config everytime you want to connect into the database. In other words, don't keep the whole connection string in a session variable, but rather a key to fetch it by.
var question = (_2b || !(_2b));
-
try doing HttpContext.Current.Session. Vishal http://openreferals.com/~vishalg.aspx[^]