Link to home
Start Free TrialLog in
Avatar of TOM BURKHARDT
TOM BURKHARDTFlag for Canada

asked on

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!!
ASKER CERTIFIED SOLUTION
Avatar of intreeg
intreeg

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TOM BURKHARDT

ASKER


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).
well, at least for databases. I have used the MapPath for other file types.
ooops .... typo, meant to say :

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

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!




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 ?

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

 Thanks, that made perfect sense!

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