how to read web.config from iis web directory

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gr8Ashish
    New Member
    • Nov 2008
    • 9

    how to read web.config from iis web directory

    hi all
    i am using a windows application as well as web application developed in visual studio 2010 beta 2 using .net framework 4 on windows xp/windows server 2008/windows7
    i am able to read web.config file on system running windows XP sp2 but same is not read by windows 7 and windows server 2008
    i installed my web application on iis 7 on said windows with .net 4 but it not read web.config by my windows application.
    i used below code on form_load in windows application for reading that web.config-
    Code:
    Dim webStr As Configuration.Configuration=WebConfigurationManager.OpenWebConfiguration("/Site/")
    Dim webConstr As String = webStr.ConnectionStrings.ConnectionStrings("connStr").ConnectionString
                    Dim getIdx As Integer = webConstr.IndexOf("User ID=", 0)
                    getIdx += 8
                    Dim getIdx2 As Integer = webConstr.IndexOf(";", getIdx)
                    Dim strLen As Integer = getIdx2 - getIdx
                    sqlUid = webConstr.Substring(getIdx, strLen)
    below is my web.config exist in c:\inetpub\wwwr oot\site\ folder -
    Code:
    <?xml version="1.0"?>
    <configuration>
    	<connectionStrings>
    		<remove name="LocalSqlServer"/>
    		<add name="LocalSqlServer" connectionString="Data Source=myMacine;Initial Catalog=ASPNETDB;Persist Security Info=True;integrated security=true" providerName="System.Data.SqlClient"/>
    		<add name="connStr" connectionString="Data Source=myMachine;Initial Catalog=abc;Persist Security Info=True;User ID=xyz;" providerName="System.Data.SqlClient"/>
    	</connectionStrings>
    .......
    ......
    </configuration>
    the error comes in reading this is -
    Code:
    Failed to map the path '/site'
    and below is the full exception path-

    Code:
    at System.Web.Configuration.ProcessHostConfigUtils.MapPathActual(String siteName, VirtualPath path)
       at System.Web.Configuration.ProcessHostMapPath.MapPathCaching(String siteID, VirtualPath path)
       at System.Web.Configuration.ProcessHostMapPath.System.Web.Configuration.IConfigMapPath2.MapPath(String siteID, VirtualPath path)
       at System.Web.Configuration.WebConfigurationHost.InitForConfiguration(String& locationSubPath, String& configPath, String& locationConfigPath, IInternalConfigRoot configRoot, Object[] hostInitConfigurationParams)
       at System.Configuration.Configuration..ctor(String locationSubPath, Type typeConfigHost, Object[] hostInitConfigurationParams)
       at System.Configuration.Internal.InternalConfigConfigurationFactory.System.Configuration.Internal.IInternalConfigConfigurationFactory.Create(Type typeConfigHost, Object[] hostInitConfigurationParams)
       at System.Web.Configuration.WebConfigurationHost.OpenConfiguration(WebLevel webLevel, ConfigurationFileMap fileMap, VirtualPath path, String site, String locationSubPath, String server, String userName, String password, IntPtr tokenHandle)
       at System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(String path)
       at appName.FormName.Form_Load(Object sender, EventArgs e) in E:\myapp\page.vb:line 130
    I hope anyone has the answer for me
    i have to install it by two day so please help me
    thanks in advanced
    Last edited by gr8Ashish; Jan 15 '10, 04:07 PM. Reason: spelling in header
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Instead of "opening" the web.config have you considered just using:

    Code:
    Dim webConstr As String = System.Configuration.ConfigurationManager.ConnectionStrings("connStr").ConnectionString
    -Frinny

    Comment

    • gr8Ashish
      New Member
      • Nov 2008
      • 9

      #3
      thanks for your reply frinny
      but it does not help and its almost same as i already did above..
      well i am not able to read connection string by above methods till now

      you or anybody has the solution pls tell me

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Did you try what I recommended?
        It may look "close" to what you're doing, but it's not the same thing.
        If you did try this, then you should have got a different error message....

        I used MSDN to look up what the OpenWebConfigur ation Method did and the documentation said that it opens the web-application configuration file as a Configuration object using the specified virtual path to allow read or write operations.

        I'm going to pretend that you just don't want to try what I recommended previously and will recommend that you try using this instead:
        Code:
        Dim webStr As Configuration.Configuration=WebConfigurationManager.OpenWebConfiguration("~/")
        Or, if your web.config file is located in the root directory of the site, don't even pass it "~/" just pass it nothing because the documentation says that if a null reference (Nothing in Visual Basic) is passed to the OpenWebConfigur ation method the root web.config file is opened....

        So you could try:
        Code:
        Dim webStr As Configuration.Configuration=WebConfigurationManager.OpenWebConfiguration()
        And it should open the root web.config file.

        -Frinny

        Comment

        • gr8Ashish
          New Member
          • Nov 2008
          • 9

          #5
          Frinny
          this method-
          Code:
          Dim webStr As Configuration.Configuration=WebConfigurationManager.OpenWebConfiguration()
          give me error like this in windows application while try to read connection string in c:\inetpub\wwwr oot\site\web.co nfig- on windows server 2008

          Code:
          NullReferenceexception was caught
          object reference not set to an instance of an object
          and your other post i try this-

          Code:
          Dim webStr As Configuration.Configuration=WebConfigurationManager.OpenWebConfiguration("/site")
          will cause
          Code:
          Failed to map the path '/site'
          and with below
          Code:
          Dim webStr As Configuration.Configuration=WebConfigurationManager.OpenWebConfiguration("~/")
          will tell that -
          Code:
          the application relative virtual path '~/' is not allowed here
          and the last one
          Code:
          Dim webStr As Configuration.Configuration=WebConfigurationManager.OpenWebConfiguration()
          will give message at compile or debug time that
          Code:
          Overlaod resolution failed becuae no accessible 'OpenWebConfiguration' accepts this number of of a arguments.
          so i am helpless ;)
          Last edited by gr8Ashish; Jan 19 '10, 12:51 PM. Reason: spell

          Comment

          • gr8Ashish
            New Member
            • Nov 2008
            • 9

            #6
            ok can you tell me how to read the web.config file's configuration section from getsection method

            ?


            ? pls giv me example
            Last edited by gr8Ashish; Jan 25 '10, 07:42 AM. Reason: edit

            Comment

            Working...