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?
 
intreegCommented:
What about the file will change?

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

0
 
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).
0
 
tom_burkhardtAuthor Commented:
well, at least for databases. I have used the MapPath for other file types.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
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.
0
 
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!




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

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

 Thanks, that made perfect sense!
0
 
tom_burkhardtAuthor Commented:

 actually, it seems this is yet another thought Microsoft has "borrowed" from the Unix systems.
0
 
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...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.