Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 8th, 2004, 04:39 PM
Registered User
 
Join Date: Apr 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Variable Path in web.Config Connection String?

I am accustomed to using Server.MapPath in connection strings as below
     objConn.Open "Driver={Microsoft Access Driver (*.mdb)};" &_
             "DBQ=" & Server.MapPath("DB\Volume.mdb")

Now in .NET applications, I am able to use the web.config file, to provide a global connection string:
   <appSettings>
      <add key = "JBulletin"
           value = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\Bulletin\DB\Volume.mdb;" />
   </appSettings>

However, is there any way to leave the path 'variable' in the web.config file, as with the MapPath method of the Server object (realizing of course that the web.config is XML and not asp script)

Thanks

 
Old April 9th, 2004, 01:50 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

You can retrieve a connection string in your app as:

objConn.Open(ConfigurationSettings.AppSettings("Co nnString") & Server.MapPath("DataSource"))

You can't trigger a server.mappath in appsettings, but you can store the path as a separate variable.
 
Old April 9th, 2004, 05:23 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Here's a clever solution that seems to work...

In your config file you store the path like this:

     Data Source={0}\DB\Volume.mdb

Then where you use the path in code, you map the root of the virtual directory and use that as the source of the replacement in String.Format:

String.Format(sPath, Server.MapPath("~"))

The ~ represents the virtual directory path. You can use this to establish a "root" reference to a resource. I.e. if you try to reference something with "/myResource" and you are running in virtual directory path "myWebApp" the resource url will reference "myServer/myResource" instead of the correct "myServer/myWebApp/myResource". ASP.net automatically fills in the ~.

So now, regardless of where the virtual directory lives on the physical machine, the {0} in the path will get replaced with the virtual directory's physical path.

Peter
-------------------------
Work smarter, not harder
 
Old April 12th, 2004, 04:58 PM
Registered User
 
Join Date: Apr 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the help. The solution I am employing is a simplistic version of those you both provided. Essentially, I am storing the database name in one variable (or "key") in web.config and then the sub-folder tree structure in a second web.config key (could as easily store them as one, but I like the separation).

This leaves the ability to change both the folder and database name centrally. In the code-behind, I use MapPath and concatenate the subfolder structure then the filename, and acheive the separability and independence betweem code and filename/folder maintenance I was looking for.

Thank you very much for the prompt and helpful replies.

ElPato

 
Old June 21st, 2004, 05:44 PM
Registered User
 
Join Date: Jun 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Could you please post the code that you ended up using? I'd like to see the solution. Thanks.





Similar Threads
Thread Thread Starter Forum Replies Last Post
connection string issues, web.config file issues kaliaparijat ASP.NET 2.0 Professional 1 June 12th, 2008 08:07 AM
how to store path in web.config appsettings sectio gbianchi ASP.NET 2.0 Professional 0 October 18th, 2006 12:37 PM
setting connection string in the web.config ACE2084 ADO.NET 2 January 6th, 2005 04:33 PM
Database Connection with Web.Config RPG SEARCH Classic ASP Databases 7 July 26th, 2004 06:10 PM
Relative Path Connection String fangai Classic ASP Databases 1 October 3rd, 2003 02:37 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.