473,395 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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-
Expand|Select|Wrap|Line Numbers
  1. Dim webStr As Configuration.Configuration=WebConfigurationManager.OpenWebConfiguration("/Site/")
  2. Dim webConstr As String = webStr.ConnectionStrings.ConnectionStrings("connStr").ConnectionString
  3.                 Dim getIdx As Integer = webConstr.IndexOf("User ID=", 0)
  4.                 getIdx += 8
  5.                 Dim getIdx2 As Integer = webConstr.IndexOf(";", getIdx)
  6.                 Dim strLen As Integer = getIdx2 - getIdx
  7.                 sqlUid = webConstr.Substring(getIdx, strLen)
  8.  
below is my web.config exist in c:\inetpub\wwwroot\site\ folder -
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <configuration>
  3.     <connectionStrings>
  4.         <remove name="LocalSqlServer"/>
  5.         <add name="LocalSqlServer" connectionString="Data Source=myMacine;Initial Catalog=ASPNETDB;Persist Security Info=True;integrated security=true" providerName="System.Data.SqlClient"/>
  6.         <add name="connStr" connectionString="Data Source=myMachine;Initial Catalog=abc;Persist Security Info=True;User ID=xyz;" providerName="System.Data.SqlClient"/>
  7.     </connectionStrings>
  8. .......
  9. ......
  10. </configuration>
  11.  
the error comes in reading this is -
Expand|Select|Wrap|Line Numbers
  1. Failed to map the path '/site'
and below is the full exception path-

Expand|Select|Wrap|Line Numbers
  1. at System.Web.Configuration.ProcessHostConfigUtils.MapPathActual(String siteName, VirtualPath path)
  2.    at System.Web.Configuration.ProcessHostMapPath.MapPathCaching(String siteID, VirtualPath path)
  3.    at System.Web.Configuration.ProcessHostMapPath.System.Web.Configuration.IConfigMapPath2.MapPath(String siteID, VirtualPath path)
  4.    at System.Web.Configuration.WebConfigurationHost.InitForConfiguration(String& locationSubPath, String& configPath, String& locationConfigPath, IInternalConfigRoot configRoot, Object[] hostInitConfigurationParams)
  5.    at System.Configuration.Configuration..ctor(String locationSubPath, Type typeConfigHost, Object[] hostInitConfigurationParams)
  6.    at System.Configuration.Internal.InternalConfigConfigurationFactory.System.Configuration.Internal.IInternalConfigConfigurationFactory.Create(Type typeConfigHost, Object[] hostInitConfigurationParams)
  7.    at System.Web.Configuration.WebConfigurationHost.OpenConfiguration(WebLevel webLevel, ConfigurationFileMap fileMap, VirtualPath path, String site, String locationSubPath, String server, String userName, String password, IntPtr tokenHandle)
  8.    at System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(String path)
  9.    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
Jan 15 '10 #1
5 9060
Frinavale
9,735 Expert Mod 8TB
Instead of "opening" the web.config have you considered just using:

Expand|Select|Wrap|Line Numbers
  1. Dim webConstr As String = System.Configuration.ConfigurationManager.ConnectionStrings("connStr").ConnectionString
-Frinny
Jan 15 '10 #2
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
Jan 18 '10 #3
Frinavale
9,735 Expert Mod 8TB
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 OpenWebConfiguration 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:
Expand|Select|Wrap|Line Numbers
  1. 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 OpenWebConfiguration method the root web.config file is opened....

So you could try:
Expand|Select|Wrap|Line Numbers
  1. Dim webStr As Configuration.Configuration=WebConfigurationManager.OpenWebConfiguration()
And it should open the root web.config file.

-Frinny
Jan 18 '10 #4
Frinny
this method-
Expand|Select|Wrap|Line Numbers
  1. Dim webStr As Configuration.Configuration=WebConfigurationManager.OpenWebConfiguration()
  2.  
give me error like this in windows application while try to read connection string in c:\inetpub\wwwroot\site\web.config- on windows server 2008

Expand|Select|Wrap|Line Numbers
  1. NullReferenceexception was caught
  2. object reference not set to an instance of an object
  3.  
and your other post i try this-

Expand|Select|Wrap|Line Numbers
  1. Dim webStr As Configuration.Configuration=WebConfigurationManager.OpenWebConfiguration("/site")
will cause
Expand|Select|Wrap|Line Numbers
  1. Failed to map the path '/site'
and with below
Expand|Select|Wrap|Line Numbers
  1. Dim webStr As Configuration.Configuration=WebConfigurationManager.OpenWebConfiguration("~/")
will tell that -
Expand|Select|Wrap|Line Numbers
  1. the application relative virtual path '~/' is not allowed here
and the last one
Expand|Select|Wrap|Line Numbers
  1. Dim webStr As Configuration.Configuration=WebConfigurationManager.OpenWebConfiguration()
will give message at compile or debug time that
Expand|Select|Wrap|Line Numbers
  1. Overlaod resolution failed becuae no accessible 'OpenWebConfiguration' accepts this number of of a arguments.
so i am helpless ;)
Jan 19 '10 #5
ok can you tell me how to read the web.config file's configuration section from getsection method

?


? pls giv me example
Jan 25 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Programmer | last post by:
Hi everyone Well here is my problem I hope you can help me
3
by: Mustaq | last post by:
Hi, How to read same config file from different application? I have 4 application in VB.NET, all are using different app.config files. NOW I need all to read only one config file, how can I do...
9
by: ALI-R | last post by:
Hi,, I have two questions : 1) Is it mandatory that config file of a desktop application must be App.config 2) Is it possible to update config file in your code?? thanks for your help. ALI
5
by: BPearson | last post by:
Hello I would like to have several sites share a single web.config file. To accomplish this, I would point the root of these sites to the same folder. Is there any reason why I might not want to...
7
by: Bob | last post by:
It's great that VS.NET makes it so effortless to add a web reference to a web service. The problem is, I haven't figured out a way to configure the URLs (or simply switch the references to another...
9
by: Benny Ng | last post by:
Hi,all, How to let the sub-directory to avoid the authentication control from Root's webconfig? I heard that we can add a new web.config to the sub-directory. And then we can slove the problem....
2
by: Luke Dalessandro | last post by:
I have an application with the following layout /root/ /root/Default.aspx /root/web.config /root/child/ /root/child/web.config web.config has a custom configuration section "testSection"...
2
by: rrossney | last post by:
Please look at the "what I've already done" section of this message before responding to it: I believe that I've done everything that the people who experience this error are typically told to do....
8
by: theWizard1 | last post by:
Using Asp.NET 1.1, and C#. I have a directory for the website, and a directory under it named Secure. I have a web.config in each of the above directories. The web.config in the Secure...
1
by: John | last post by:
Hi, Trying to troubleshoot a web app. I'm getting the usual "set your web.config file to customErrors mode="Off"" message. Anyway, I DO have a web.config file in the root of the app, but it's...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.