473,399 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Database connection string in web.config

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>

Nov 17 '05 #1
4 17640
> 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.

"re**************@nospam.com" <se*@website.com> wrote in message
news:d3********************************@4ax.com...
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>

Nov 17 '05 #2
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.


On Thu, 6 Nov 2003 09:55:50 -0500, "Kevin Spencer"
<ke***@DIESPAMMERSDIEtakempis.com> wrote:
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.


Nov 17 '05 #3
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.

"re**************@nospam.com" <se*@website.com> wrote in message
news:mm********************************@4ax.com...
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.


On Thu, 6 Nov 2003 09:55:50 -0500, "Kevin Spencer"
<ke***@DIESPAMMERSDIEtakempis.com> wrote:
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 createthe variable as an Application Cache value instead.

Nov 17 '05 #4
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!
On Thu, 6 Nov 2003 17:24:48 -0500, "Kevin Spencer"
<ke***@DIESPAMMERSDIEtakempis.com> wrote:
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"];


Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Macca | last post by:
Hi, I have an windows forms application that accesses a SQL database I have a few questions as to connecting to the database. This application will run 24 hours a day. It is a monitoring...
5
by: Matt | last post by:
Hello, What is the best way to handle the database connection string for a class library project that will be compiled and used as a .dll? This .dll will be accessed via classic ASP and in...
3
by: josh.kuo | last post by:
Sorry about the subject, I can't think of a better one. I recently wrote some PHP classes that I think might be of interest to this group. Since I have been reaping the benefits of reading news...
3
by: jason | last post by:
I've been playing around with new (for 2.0) membershp functionality. I was able to build a simple login form that secures a directory on a project I built locally on my development desktop. ...
2
by: Patrick F | last post by:
Hi, i have SQL Server 2005 and a database set that is called, myCompany the problem is that i cant connect from my page to it, here is from the web.config: ( i have got this connectionstring from...
4
by: shapper | last post by:
Hello, I am using Microsoft Enterprise Library, on a class, to connect to a database as follows: Dim db As Database = DatabaseFactory.CreateDatabase("DatabaseName") However, I would like...
5
by: Edwin Smith | last post by:
Hello: I'm using VS2005 with an ODBC database. I am trying to add a Dialog to an existing Database application to allow the user to select the database. We have 2 different databases with the...
7
by: =?Utf-8?B?TWF4R3J1dmVu?= | last post by:
I have a DLL that implements the Business and Data Layers for a number of different websites. That DLL uses a Strongly Typed DataSet to interface to the Data Source which is SQL Server. There...
10
by: jimjim | last post by:
HI, We have shifted our database from sqlserver2003 to sqlserver2005. 1. connection strings stored in web.config <!-- Machine name to connection string keys --> <add key="*777*"...
12
lifeisgreat20009
by: lifeisgreat20009 | last post by:
I am a newbie to Struts and JSP...I have been working on the code below for 5 hours now..I googled a lot but couldn't get much help so finally I am here.. Hoping of getting my problem solved. Please...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.