473,473 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SQL Connection in app.config question...

I'd like to specify the connection string to the SQL Server so that I can just
refer to it instead of the full connection string.

Here's what I have:
<app.config>
<appSettings>
<add key="SQLDBConn",value="Server=SqlSrvr;Database=DBN ame;User
ID=uid;Password=pwd;Trusted_Connection=False" />
</appSettings>

Then in my code I replace the connection string with the key "SQLDBConn":
SqlConnection conn = new SqlConnection(SQLDBConn);

But I get an error when I compile that it does not exist in the namespace.

What have I done wrong?

Thanks.
Apr 11 '07 #1
4 6680
Is it a .NET 1.0 or 2.0 application?
Apr 11 '07 #2
Joe,

string sqlDbConn = ConfigurationManager.AppSettings["SQLDBConn"];

1) need a reference to System.Configuration assembly.
2) you can make this static and put it in Global.asax (at class level) and
refer to it from any page as "Global.sqlDbConn". You can also just store it
in Application State by reading and assigning in the Global Application_Start
handler method.
3) This should really have been in the ASP.NET group, but I guess that's a
lost cause.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Joe" wrote:
I'd like to specify the connection string to the SQL Server so that I can just
refer to it instead of the full connection string.

Here's what I have:
<app.config>
<appSettings>
<add key="SQLDBConn",value="Server=SqlSrvr;Database=DBN ame;User
ID=uid;Password=pwd;Trusted_Connection=False" />
</appSettings>

Then in my code I replace the connection string with the key "SQLDBConn":
SqlConnection conn = new SqlConnection(SQLDBConn);

But I get an error when I compile that it does not exist in the namespace.

What have I done wrong?

Thanks.

Apr 11 '07 #3
1.0. Does that make a difference?

"Naeem" wrote:
Is it a .NET 1.0 or 2.0 application?
Apr 11 '07 #4
The way you access the connection string from the web.config is different. I
see that Peter Bromberg has posted the solution, that should help you.

"Joe" wrote:
1.0. Does that make a difference?

"Naeem" wrote:
Is it a .NET 1.0 or 2.0 application?
Apr 12 '07 #5

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

Similar topics

6
by: VK | last post by:
Dear All: We are in the processing of building a new web site using VB.NET and SQL2000 I had a question about WHERE to store the connection string for SQL dB - in an include file or...
6
by: H | last post by:
This is a question that has haunted me for quite some time. if you build a 4 tier database application where the 4th tier is the database server (MS SQL 2000), where do you build the connection...
6
by: Jon Davis | last post by:
I like the drag-and-drop accessibility of dragging a table to a Web Forms designer and seeing a SqlDataAdapter automatically created for me.. being able to create a DataSet from that is fun and...
19
by: Jaime Stuardo | last post by:
Hi all.. I have created a business logic component that is used from my ASP.NET webform. It works, but connection string to the database is hard coded, as in this method : public DataSet...
9
by: nbs.tag | last post by:
hey guys heres my question. I was told by a little birdie that .net 2.0 has the ability to read a connection string directly from a registry key. so in the registry key a string value of say :...
1
by: Jon | last post by:
Hello all - just a spot of advice required. I'm wanting to write a number of data layers that will be used to carry out the typical CRUD operations against a database - so 1 database will be...
9
by: =?Utf-8?B?Vmlua2k=?= | last post by:
Hello Everyone, I have a question about storing the connection strings to the database in a config file or database. My manager wants me to store all the connection strings in a database, but I...
2
by: Thorsten Dittmar | last post by:
Hi, I don't get it. I'm using a typed dataset with table adapters and all that stuff. I have the database server running locally on my development system. Now: when creating a tableadapter...
6
by: CSharper | last post by:
We have a good size project and there we will connect to sql from different projects. What is the best practice to connect to sql? I am pretty sure, writing the hard coded is not a good solution. I...
0
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,...
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
1
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
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 ...
1
muto222
php
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.