web.config Server.MapPath connection string

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!!
tom_burkhardtAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

intreegCommented:
What about the file will change?

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tom_burkhardtAuthor 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).
tom_burkhardtAuthor Commented:
well, at least for databases. I have used the MapPath for other file types.
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

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

well, at least for databases. I have used the MapPath with relative path for other file types.
intreegCommented:
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!




tom_burkhardtAuthor 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 ?
tom_burkhardtAuthor Commented:

or is the ~/ only for relative links within a site?
intreegCommented:
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.
tom_burkhardtAuthor Commented:

 Thanks, that made perfect sense!
tom_burkhardtAuthor Commented:

 actually, it seems this is yet another thought Microsoft has "borrowed" from the Unix systems.
intreegCommented:
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...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.