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. Global variables where do they go?

Global variables where do they go?

Scheduled Pinned Locked Moved ASP.NET
csharpquestionasp-netdatabase
6 Posts 5 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.
  • M Offline
    M Offline
    Manster
    wrote on last edited by
    #1

    Hi, I'm new to ASP.NET and I'm trying to create an (C#) application that uses global variables. I have a database connection string and I want to be able to use it in many of my pages. How can I do this? I created an object to do this, but I don't want to be forced to create that object on every page which uses the database. Thanks :)

    M C 2 Replies Last reply
    0
    • M Manster

      Hi, I'm new to ASP.NET and I'm trying to create an (C#) application that uses global variables. I have a database connection string and I want to be able to use it in many of my pages. How can I do this? I created an object to do this, but I don't want to be forced to create that object on every page which uses the database. Thanks :)

      M Offline
      M Offline
      Mazdak
      wrote on last edited by
      #2

      You can use Session to store global variables but thats not goood idea for connection string because if you want to change it you need to recompilation so it is better to store it in web.config file located in your root directory.Add this key at the end of it before closing Configuration tag: Whenever you want to get this value you can get it like this: string str = ConfigurationSettings.AppSettings["Connection_Setting"];

      H 1 Reply Last reply
      0
      • M Mazdak

        You can use Session to store global variables but thats not goood idea for connection string because if you want to change it you need to recompilation so it is better to store it in web.config file located in your root directory.Add this key at the end of it before closing Configuration tag: Whenever you want to get this value you can get it like this: string str = ConfigurationSettings.AppSettings["Connection_Setting"];

        H Offline
        H Offline
        hxxbin
        wrote on last edited by
        #3

        The Object Session is global.asax's OnStartApplication??? I remember a example into the SDK documentation. ---- hxxbin

        S M 2 Replies Last reply
        0
        • H hxxbin

          The Object Session is global.asax's OnStartApplication??? I remember a example into the SDK documentation. ---- hxxbin

          S Offline
          S Offline
          Steve McLenithan
          wrote on last edited by
          #4

          Mazdak's suggestion would probably be more efficient.

          // Steve McLenithan

          Cluelessnes:
             There are no stupid questions, but there are a lot of inquisitive idiots.

          1 Reply Last reply
          0
          • M Manster

            Hi, I'm new to ASP.NET and I'm trying to create an (C#) application that uses global variables. I have a database connection string and I want to be able to use it in many of my pages. How can I do this? I created an object to do this, but I don't want to be forced to create that object on every page which uses the database. Thanks :)

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            I'd go with Mazdax on putting the connection string in the AppSetting section of the web.config file. However, what I'd also do is set up a Singleton class (that is a class where only one instance ever exists) to manage your global configuration stuff. e.g.

            class AppConfiguration
            {
            public static string ConnectionString
            {
            get
            {
            return System.Configuration.ConfigurationSettings.AppSettings["DbConnectionString"];
            }
            }

            public static SqlConnection
            {
                get
                {
                    return new SqlConnection(ConnectionString);
                }
            }
            

            }

            This will also create your connection object for you too - Which produces much cleaner code. --Colin Mackay--

            1 Reply Last reply
            0
            • H hxxbin

              The Object Session is global.asax's OnStartApplication??? I remember a example into the SDK documentation. ---- hxxbin

              M Offline
              M Offline
              Mazdak
              wrote on last edited by
              #6

              Session is usable and can be stareted where ever you want but that is very bad way for connection string. Mazy No sig. available now.

              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