473,795 Members | 2,746 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="Connection String"
value="Provider =Microsoft.Jet. OLEDB.4.0;Data Source=" &
Server.MapPath( ".") & "DATABASE\test. mdb;" />

</appSettings>

Nov 17 '05 #1
4 17659
> 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_OnS tart 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.co m> wrote in message
news:d3******** *************** *********@4ax.c om...
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="Connection String"
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("Co nnectionString" ) =
"Provider=Micro soft.Jet.OLEDB. 4.0;Data Source=" &
Application("Ph ysPath") & "\test.mdb; "

then the code

Dim conOleDb As New OleDb.OleDbConn ection()
conOleDb.Connec tionString = Application("Co nnectionString" )

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***@DIESPAMM ERSDIEtakempis. 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_OnS tart 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 ConnectionStrin g = HttpContext.Cur rent.Cache["ConnectionStri ng"];

--
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.co m> wrote in message
news:mm******** *************** *********@4ax.c om...
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("Co nnectionString" ) =
"Provider=Micro soft.Jet.OLEDB. 4.0;Data Source=" &
Application("Ph ysPath") & "\test.mdb; "

then the code

Dim conOleDb As New OleDb.OleDbConn ection()
conOleDb.Connec tionString = Application("Co nnectionString" )

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***@DIESPAMM ERSDIEtakempis. 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_OnS tart 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.Cu rrent.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***@DIESPAMM ERSDIEtakempis. 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 ConnectionStrin g = HttpContext.Cur rent.Cache["ConnectionStri ng"];


Nov 17 '05 #5

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

Similar topics

4
2475
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 application and will store events that happen in the database (These events happen randomly without pattern, between 10-50 a day) . There are a number of situations where the database is accessed.
5
2133
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 the future by ASP.NET pages. I have created a constant that contains the connection string (as shown below).
3
2947
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 groups for years, I figure it's time for me to contribute a little bit back, maybe some people out there will find this useful. * Introduction This is a "how-to" style article, showing by example how to dynamically
3
4166
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. However, when I attempt to follow the same steps on the remote test server (a website I access via filesystem security accross my lan ).. I get the following when I attempt to select the security tab from the administer selection of the login...
2
4128
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 my sqldataobject on my page) <add name="ConStr" connectionString="Data Source=localhost;Initial Catalog=myCompany;Integrated Security=True" providerName="System.Data.SqlClient" /> the strange part is that its trying to connect to a SQL express...
4
4521
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 the database name to be dynamic, i.e., specified on the web site.
5
5808
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 same schema so this is a very desirable function. I can configure 2 different connection strings in app,config but I can't figure out how to allow the user to select one or the other.
7
1741
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 is a connection string in the DLL that is causes me grief. It points at a particular instance of database. I hae to recompile the DLL for each WebSite. How can I re-use that DLL on different websites and have it point to different instances of...
10
2320
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*" value="**ConnString_DEV" /> <add key="server2" value="**ConnString_TST" /> <add key="server1" value="**ConnString_PRD" /> <!-- Literal Connection String Keys -->
12
78028
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 give me some idea where I am going wrong ?? I just want to retrieve data from my emp_mstr table and display it using my JSP file... The table emp_mstr is as follows :- CREATE TABLE EMP_MSTR( EMP_NO VARCHAR(10) PRIMARY KEY, PASSWORD...
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10213
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10000
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9040
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3722
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.