Database connection string in web.config

  • Thread starter refer_to_website
  • Start date
R

refer_to_website

How can I use the Server.MapPath(".") value inside of the web.config?
For example, when I add the following just before the
</configuration>, and run via CTRL-F5, I receive error
The '&' character, hexadecimal value 0x26, cannot begin with a name.

<appSettings>

<add key="ConnectionString"
value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
Server.MapPath(".") & "DATABASE\test.mdb;" />

</appSettings>
 
K

Kevin Spencer

How can I use the Server.MapPath(".") value inside of the web.config?

You can't. You can only store static data in the web.config file.

You might want to use the global.asa Application_OnStart method and create
the variable as an Application Cache value instead.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Big Things are made up of
Lots of Little Things.
 
R

refer_to_website

When I declared the connection string inside of global.asa, my
application's FORMS can access it fine, but it is out of scope from
the VB class modules. For example, when I declare this in the
global.asa:

Application("ConnectionString") =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
Application("PhysPath") & "\test.mdb;"

then the code

Dim conOleDb As New OleDb.OleDbConnection()
conOleDb.ConnectionString = Application("ConnectionString")

works great inside a form's code behind, but the Application object is
not accessible from a class module.

Where and how is the best place to declare a connection string to an
Access database which is accessible to both forms and classes, and
whose location is not hard-coded (i.e. use Server.MapPath(".") to
identify the database's location)?

Thanks.
 
K

Kevin Spencer

Okay, first, I mentioned the Application Cache, which is different than the
Application Collection. This is referred to in an ASP.Net page as the
"Cache" property. It is thread-safe, and recommended.

Secondly, you can certainly reference the Application and the Application
Cache in any class that is used by an ASP.Net Page or application. Just
refer to the Current HttpContext to get it. Example:

string ConnectionString = HttpContext.Current.Cache["ConnectionString"];

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Big Things are made up of
Lots of Little Things.
 
R

refer_to_website

Thanks Kevin. VB.NET did not recognize the ".cache" in
"HttpContext.Current.Cache", but I was able to figure out how to
access the connection string in both code-behind and class modules by
researching the HttpContext object. Thanks for showing me the path!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top