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. General Programming
  3. C#
  4. web.config file access in another project

web.config file access in another project

Scheduled Pinned Locked Moved C#
visual-studioquestionworkspace
9 Posts 3 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
    vkEE
    wrote on last edited by
    #1

    Hello All, Our main 2012 VS solution has 16 projects. One of the project is a web site, which a web.config file defined. I want to access this website's web.config file's applicationSettings value in another project. I added the System.Configuration to this class. And using below trying to access the value of the settings. Since this setting is defined as a string, converting it to an int. The value for the setting QuesId is 2 in the web.config file. The code complies with no errors, but when i execute and browse the code, the value is null. any ideas??

    Int32.Parse(ConfigurationManager.AppSettings["QuesId"]);

    Richard DeemingR 1 Reply Last reply
    0
    • V vkEE

      Hello All, Our main 2012 VS solution has 16 projects. One of the project is a web site, which a web.config file defined. I want to access this website's web.config file's applicationSettings value in another project. I added the System.Configuration to this class. And using below trying to access the value of the settings. Since this setting is defined as a string, converting it to an int. The value for the setting QuesId is 2 in the web.config file. The code complies with no errors, but when i execute and browse the code, the value is null. any ideas??

      Int32.Parse(ConfigurationManager.AppSettings["QuesId"]);

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      How are you executing the other project? If it's a class library called from within the web site, the code you've posted should work. If it's a stand-alone application, it will need its own configuration file.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      M 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        How are you executing the other project? If it's a class library called from within the web site, the code you've posted should work. If it's a stand-alone application, it will need its own configuration file.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        M Offline
        M Offline
        Marco Bertschi
        wrote on last edited by
        #3

        Richard Deeming wrote:

        If it's a class library called from within the web site, the code you've posted should work. If it's a stand-alone application, it will need its own configuration file.

        Methinks that he has a config file for the library {1}, and one for the web app{2}. And now he has troubles accessing {1} when the library method is executed from the web app since the library will try to access {2}.

        Clean-up crew needed, grammar spill... - Nagy Vilmos

        V 1 Reply Last reply
        0
        • M Marco Bertschi

          Richard Deeming wrote:

          If it's a class library called from within the web site, the code you've posted should work. If it's a stand-alone application, it will need its own configuration file.

          Methinks that he has a config file for the library {1}, and one for the web app{2}. And now he has troubles accessing {1} when the library method is executed from the web app since the library will try to access {2}.

          Clean-up crew needed, grammar spill... - Nagy Vilmos

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

          In the web.config file, if I use the then I am able to access this setting in my other C# project using, with no issues:

          Int32.Parse(ConfigurationManager.AppSettings["QId"]);

          But if it is defined in the part of web.config, when I try to access it in my other C# project:

          Int32.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings["QId"])

          a null value is returned. Is my syntax incorrect?

          M 1 Reply Last reply
          0
          • V vkEE

            In the web.config file, if I use the then I am able to access this setting in my other C# project using, with no issues:

            Int32.Parse(ConfigurationManager.AppSettings["QId"]);

            But if it is defined in the part of web.config, when I try to access it in my other C# project:

            Int32.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings["QId"])

            a null value is returned. Is my syntax incorrect?

            M Offline
            M Offline
            Marco Bertschi
            wrote on last edited by
            #5

            No. The problem most likely is that

            Int32.Parse(ConfigurationManager.AppSettings["QId"]);

            is accessing the app.config file, while

            Int32.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings["QId"])

            is accessing the web.config. Furthermore, if you have two configuration files, only the file of the project you build as Startup project is read by either ConfigurationManager.AppSettings or WebConfigurationManager.AppSettings.

            Clean-up crew needed, grammar spill... - Nagy Vilmos

            V 1 Reply Last reply
            0
            • M Marco Bertschi

              No. The problem most likely is that

              Int32.Parse(ConfigurationManager.AppSettings["QId"]);

              is accessing the app.config file, while

              Int32.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings["QId"])

              is accessing the web.config. Furthermore, if you have two configuration files, only the file of the project you build as Startup project is read by either ConfigurationManager.AppSettings or WebConfigurationManager.AppSettings.

              Clean-up crew needed, grammar spill... - Nagy Vilmos

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

              Actually both settings are defined in the web.config file, but under different sections. I am not using the app.config file in my other project.

              M 1 Reply Last reply
              0
              • V vkEE

                Actually both settings are defined in the web.config file, but under different sections. I am not using the app.config file in my other project.

                M Offline
                M Offline
                Marco Bertschi
                wrote on last edited by
                #7

                Can you try to use ConfigurationManager.AppSettings instead of WebSettings?

                Clean-up crew needed, grammar spill... - Nagy Vilmos

                V 1 Reply Last reply
                0
                • M Marco Bertschi

                  Can you try to use ConfigurationManager.AppSettings instead of WebSettings?

                  Clean-up crew needed, grammar spill... - Nagy Vilmos

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

                  Null value is returned.

                  M 1 Reply Last reply
                  0
                  • V vkEE

                    Null value is returned.

                    M Offline
                    M Offline
                    Marco Bertschi
                    wrote on last edited by
                    #9

                    Can you post the config file?

                    Clean-up crew needed, grammar spill... - Nagy Vilmos

                    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