Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Static property of class

Static property of class

Scheduled Pinned Locked Moved ASP.NET
helpdatabase
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vnsraj
    wrote on last edited by
    #1

    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

    V G N 3 Replies Last reply
    0
    • V vnsraj

      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

      V Offline
      V Offline
      vishalg_gupta
      wrote on last edited by
      #2

      you can keep the properties in session variables. Vishal http://openreferals.com/~vishalg.aspx[^]

      V 1 Reply Last reply
      0
      • V vnsraj

        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

        G Offline
        G Offline
        Greg Chelstowski
        wrote on last edited by
        #3

        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));

        1 Reply Last reply
        0
        • V vishalg_gupta

          you can keep the properties in session variables. Vishal http://openreferals.com/~vishalg.aspx[^]

          V Offline
          V Offline
          vnsraj
          wrote on last edited by
          #4

          In class file session is not working properly.....

          V 1 Reply Last reply
          0
          • V vnsraj

            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

            N Offline
            N Offline
            N a v a n e e t h
            wrote on last edited by
            #5

            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, Use session variables. These are unique for each visitor. :)

            Navaneeth How to use google | Ask smart questions

            V 1 Reply Last reply
            0
            • N N a v a n e e t h

              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, Use session variables. These are unique for each visitor. :)

              Navaneeth How to use google | Ask smart questions

              V Offline
              V Offline
              vnsraj
              wrote on last edited by
              #6

              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; } } } }

              G 1 Reply Last reply
              0
              • V vnsraj

                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; } } } }

                G Offline
                G Offline
                Greg Chelstowski
                wrote on last edited by
                #7

                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));

                1 Reply Last reply
                0
                • V vnsraj

                  In class file session is not working properly.....

                  V Offline
                  V Offline
                  vishalg_gupta
                  wrote on last edited by
                  #8

                  try doing HttpContext.Current.Session. Vishal http://openreferals.com/~vishalg.aspx[^]

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups