We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

web.config  Server.MapPath connection string

tom_burkhardt
on
Medium Priority
4,286 Views
Last Modified: 2010-05-18
Hi,

I'm new to Asp.NET, up to now have been using plainold ASP.

Is it possible (YES or NO) to use the Server.MapPath function in the Web.Config file?

I'm asking for the obvious reason of not having to change everything when I copy files from my test server to a live site.

Right now on my ASPX page, I have:
Dim connectionString As String = ConfigurationSettings.AppSettings("ConnectionString")
Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)

and in my Web.Config file, I have tried to use :
<appSettings>
   <add key="ConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0;Ole DB Services=-4;Data Source=" & Server.MapPath("data\mydatbase.mdb") & ";" />
</appSettings>

This gives me an error message.

If it is NOT possible to use Server.MapPath in the Web.Config file, do I have to use something like :
<appSettings>
   <add key="ConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0;Ole DB Services=-4;Data Source={0}" />
</appSettings>

and then how would I call that from my aspx page:
Dim connectionString As String = ConfigurationSettings.AppSettings("ConnectionString")????????

thanx for any help!!
Comment
Watch Question

Commented:
What about the file will change?

If you use something like: Data Source="data/mydatbase.mdb", it does not find the file?

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:

sonofagun, that worked!!!!
Didn't think you could use a relative path for Server.Mappath; I've always thought you had to put the entire physical path starting from the root directory(i.e:C:\Inetpub\myfoldermysubfolder\etc).

Author

Commented:
well, at least for databases. I have used the MapPath for other file types.

Author

Commented:
ooops .... typo, meant to say :

well, at least for databases. I have used the MapPath with relative path for other file types.

Commented:
I think the web.config accepts either full or relative for most things...

also just fyi (though you might already know this) the new syntax for web site root is ~/ instead of doing ../../ etc.


Thanks for the A btw!




Author

Commented:
Thanks for the extra comment, no I was not aware of that.

So, if I understand correctly
~/mysite/default.htm

would replace
C:\inetpub\mysite\default.htm ?

Author

Commented:

or is the ~/ only for relative links within a site?

Commented:
Not exactly, more like...

If the full Filesystem path to your webfolder is c:\intepub\mysite, then you have a subfolder called sub1 that contains file1.aspx. You can reference the file1.aspx from anywhere in your site by using "~/sub1/file1.aspx" or to get your "default.aspx" in your root you would use "~/default.aspx"

Its just a shortcut from doing ../default.aspx from a file in the sub1 folder.

I have a feeling im not explaing this very well... i hope that made sense. It is only used for relative paths inside of the website.

Author

Commented:

 Thanks, that made perfect sense!

Author

Commented:

 actually, it seems this is yet another thought Microsoft has "borrowed" from the Unix systems.

Commented:
lol, very true... but really can we complain about that? Vista looks to use more than a few 'Nix concepts... and right on! Maybe we can count down on the virus, worm etc epidemic using some good ol' fashion DONT RUN AS ROOT JACK@$$ theory.....

This conversation can easily and has started to drift, so I will leave it at that...
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.