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. Problem in accessing Application variable

Problem in accessing Application variable

Scheduled Pinned Locked Moved ASP.NET
helpbusiness
9 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.
  • K Offline
    K Offline
    krishsenthilraj
    wrote on last edited by
    #1

    hi all, I have created an Application level connection string variable in my Global.asax Like: Application("glbConnection") = con.getDBConnection() I accessed that variable in My Business class like, con As IDbConnection = Application("glbConnection") But i Got the Error as , System.NullReferenceException {"Object reference not set to an instance of an object."} Message : "Object reference not set to an instance of an object." I need to access my application variable in my Class file. can anyone help me out to solve this problem Thanks in advance. Regards senthilraj K

    N E M 3 Replies Last reply
    0
    • K krishsenthilraj

      hi all, I have created an Application level connection string variable in my Global.asax Like: Application("glbConnection") = con.getDBConnection() I accessed that variable in My Business class like, con As IDbConnection = Application("glbConnection") But i Got the Error as , System.NullReferenceException {"Object reference not set to an instance of an object."} Message : "Object reference not set to an instance of an object." I need to access my application variable in my Class file. can anyone help me out to solve this problem Thanks in advance. Regards senthilraj K

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

      Try Context.Application


      My Website | Ask smart questions

      1 Reply Last reply
      0
      • K krishsenthilraj

        hi all, I have created an Application level connection string variable in my Global.asax Like: Application("glbConnection") = con.getDBConnection() I accessed that variable in My Business class like, con As IDbConnection = Application("glbConnection") But i Got the Error as , System.NullReferenceException {"Object reference not set to an instance of an object."} Message : "Object reference not set to an instance of an object." I need to access my application variable in my Class file. can anyone help me out to solve this problem Thanks in advance. Regards senthilraj K

        E Offline
        E Offline
        Eduard Keilholz
        wrote on last edited by
        #3

        To solve your current problem : I guess con.getDBConnection() returns a null value To solve your next problem, I don't think you want to connect to your database throughout your website this way. You say you have a business class. The business class should be in a separate project (your business project) which contains all db functionality and stuff and returns the required information to your user interface (in this case a website). In theory, you're website shouldn't be able to 'know' your connection string.

        .: I love it when a plan comes together :. http://www.zonderpunt.nl

        K 1 Reply Last reply
        0
        • E Eduard Keilholz

          To solve your current problem : I guess con.getDBConnection() returns a null value To solve your next problem, I don't think you want to connect to your database throughout your website this way. You say you have a business class. The business class should be in a separate project (your business project) which contains all db functionality and stuff and returns the required information to your user interface (in this case a website). In theory, you're website shouldn't be able to 'know' your connection string.

          .: I love it when a plan comes together :. http://www.zonderpunt.nl

          K Offline
          K Offline
          krishsenthilraj
          wrote on last edited by
          #4

          i am getting the connection string in my code behind file when i use the appliaction variable (webform1.aspx.vb) When i try this with my class i am getting the error. Do i need to import any namespace in my class file to access this application variable in my class?? thanks, Senthilraj K

          N E 2 Replies Last reply
          0
          • K krishsenthilraj

            i am getting the connection string in my code behind file when i use the appliaction variable (webform1.aspx.vb) When i try this with my class i am getting the error. Do i need to import any namespace in my class file to access this application variable in my class?? thanks, Senthilraj K

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

            Check my first reply


            My Website | Ask smart questions

            K 1 Reply Last reply
            0
            • K krishsenthilraj

              i am getting the connection string in my code behind file when i use the appliaction variable (webform1.aspx.vb) When i try this with my class i am getting the error. Do i need to import any namespace in my class file to access this application variable in my class?? thanks, Senthilraj K

              E Offline
              E Offline
              Eduard Keilholz
              wrote on last edited by
              #6

              Erhm, in this case, you can better create a function which connects to the database for you in your businessfile public void ConnectDb(object ConnectionString) { if (ConnectionString == null) return; con = new con(ConnectionString.ToString()); } and call that function from your webpage ConnectDb(Application["yourvariable"]);

              .: I love it when a plan comes together :. http://www.zonderpunt.nl

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

                Check my first reply


                My Website | Ask smart questions

                K Offline
                K Offline
                krishsenthilraj
                wrote on last edited by
                #7

                hi navaneeth, i tried with your option.but, when i try to type the Context.Application i am getting the error as 'Context' is not accessible. I have imported the 'System.Web.HttpContext' in my class.but still getting the error.... do you have any sample code to do this???? thanks, Senthilraj K

                1 Reply Last reply
                0
                • K krishsenthilraj

                  hi all, I have created an Application level connection string variable in my Global.asax Like: Application("glbConnection") = con.getDBConnection() I accessed that variable in My Business class like, con As IDbConnection = Application("glbConnection") But i Got the Error as , System.NullReferenceException {"Object reference not set to an instance of an object."} Message : "Object reference not set to an instance of an object." I need to access my application variable in my Class file. can anyone help me out to solve this problem Thanks in advance. Regards senthilraj K

                  M Offline
                  M Offline
                  Mircea Grelus
                  wrote on last edited by
                  #8

                  For security reasons you should define your connection strings in web.Config, and access then using the ConfigurationManager.ConnectionStrings[^] properties

                  Cheers, Mircea Many people spend their life going to sleep when they’re not sleepy and waking up while they still are.

                  E 1 Reply Last reply
                  0
                  • M Mircea Grelus

                    For security reasons you should define your connection strings in web.Config, and access then using the ConfigurationManager.ConnectionStrings[^] properties

                    Cheers, Mircea Many people spend their life going to sleep when they’re not sleepy and waking up while they still are.

                    E Offline
                    E Offline
                    Eduard Keilholz
                    wrote on last edited by
                    #9

                    But using web.config for only a connectionstring isn't really the neat way, because handling a web.config is relatively pretty time consuming...

                    .: I love it when a plan comes together :. http://www.zonderpunt.nl

                    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