473,480 Members | 4,827 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to store connection string in congif file

Hi,
I am using SQL Server as a Database in my ASP.NET application. Can anyone
tell me, how to store my connection string in web.config file and access it
from my application.
It is very urgent please..

--
Perin
Nov 18 '05 #1
6 1299
Hi Perin,
In web.config file, add the section below.

<configuration>
<appSettings>
<add key="ConnectionString"
value="server=localhost;database=testdb;uid=sa;pas sword=secret;" />
</appSettings>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>

In your code, access like this..

strConnection = ConfigurationSettings.AppSettings("ConnectionStrin g")
sqlConn = New SqlConnection(strConnection)
sqlCmd = New SqlCommand("SELECT * FROM Customers", sqlConn)
sqlConn.Open()

Very simple..

Rgds,
John Paul. A

"Perin" wrote:
Hi,
I am using SQL Server as a Database in my ASP.NET application. Can anyone
tell me, how to store my connection string in web.config file and access it
from my application.
It is very urgent please..

--
Perin

Nov 18 '05 #2
This method is highlighted in the Microsoft Inovation Day

SQL Connection String in web.config using DPAPI:

http://msdn.microsoft.com/library/de...SecNetHT08.asp

http://builder.com.com/5100-6373_14-...ml#Listing%20A

"Perin" wrote:
Hi,
I am using SQL Server as a Database in my ASP.NET application. Can anyone
tell me, how to store my connection string in web.config file and access it
from my application.
It is very urgent please..

--
Perin

Nov 18 '05 #3
Leo
Just remember to add the following import statement at the top of any
module/Codebehind/class that will be using

ConfigurationSettings.AppSettings("ConnectionStrin g")
Imports System.Configuration.ConfigurationSettings

"John Paul. A" <Jo*******@discussions.microsoft.com> wrote in message
news:13**********************************@microsof t.com...
Hi Perin,
In web.config file, add the section below.

<configuration>
<appSettings>
<add key="ConnectionString"
value="server=localhost;database=testdb;uid=sa;pas sword=secret;" />
</appSettings>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>

In your code, access like this..

strConnection = ConfigurationSettings.AppSettings("ConnectionStrin g")
sqlConn = New SqlConnection(strConnection)
sqlCmd = New SqlCommand("SELECT * FROM Customers", sqlConn)
sqlConn.Open()

Very simple..

Rgds,
John Paul. A

"Perin" wrote:
Hi,
I am using SQL Server as a Database in my ASP.NET application. Can anyone
tell me, how to store my connection string in web.config file and access
it
from my application.
It is very urgent please..

--
Perin

Nov 18 '05 #4
In web.Config you have an <appSettings> section.
Insert following lines
<add key="CS"
value=" Write connection string here." />

From codebehind pages access CS by
Me.SQLConnection1.ConnectionString =
System.Configuration.ConfigurationSettings.AppSett ings("CS")

HTH.
Regards
Jignesh.

"Perin" <ja****************@hotmail.com> wrote in message
news:9A**********************************@microsof t.com...
Hi,
I am using SQL Server as a Database in my ASP.NET application. Can anyone
tell me, how to store my connection string in web.config file and access it from my application.
It is very urgent please..

--
Perin

Nov 18 '05 #5
Hello John,

Even better would be a class that wraps the ConfigurationSettings, so that
you arent relying on magic constants...

class MyConfiguration
{
public static string ConnectionString { get { return ConfigurationSettings.AppSettings["ConnectionString"];
} }
}

then: SqlConnection conn = new SqlConnection(MyConfiguration.ConnectionString);

This way, errors are caught at compile time, rather than runtime. Also, if
you happen to change the name of the setting in the app.config, you can change
it in one spot, rather than having to do a global search/replace.

--
Matt Berther
http://www.mattberther.com
Hi Perin,
In web.config file, add the section below.
<configuration>
<appSettings>
<add key="ConnectionString"
value="server=localhost;database=testdb;uid=sa;pas sword=secret;"
/>
</appSettings>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>
In your code, access like this..

strConnection = ConfigurationSettings.AppSettings("ConnectionStrin g")
sqlConn = New SqlConnection(strConnection)
sqlCmd = New SqlCommand("SELECT * FROM Customers", sqlConn)
sqlConn.Open()
Very simple..

Rgds,
John Paul. A
"Perin" wrote:
Hi,
I am using SQL Server as a Database in my ASP.NET application. Can
anyone
tell me, how to store my connection string in web.config file and
access it
from my application.
It is very urgent please..
-- Perin

Nov 18 '05 #6
Dear MAtt Berther,
What you are saying is right.
Thanks for your guidance.

Rgds,
John Paul. A

"Matt Berther" wrote:
Hello John,

Even better would be a class that wraps the ConfigurationSettings, so that
you arent relying on magic constants...

class MyConfiguration
{
public static string ConnectionString { get { return ConfigurationSettings.AppSettings["ConnectionString"];
} }
}

then: SqlConnection conn = new SqlConnection(MyConfiguration.ConnectionString);

This way, errors are caught at compile time, rather than runtime. Also, if
you happen to change the name of the setting in the app.config, you can change
it in one spot, rather than having to do a global search/replace.

--
Matt Berther
http://www.mattberther.com
Hi Perin,
In web.config file, add the section below.
<configuration>
<appSettings>
<add key="ConnectionString"
value="server=localhost;database=testdb;uid=sa;pas sword=secret;"
/>
</appSettings>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>
In your code, access like this..

strConnection = ConfigurationSettings.AppSettings("ConnectionStrin g")
sqlConn = New SqlConnection(strConnection)
sqlCmd = New SqlCommand("SELECT * FROM Customers", sqlConn)
sqlConn.Open()
Very simple..

Rgds,
John Paul. A
"Perin" wrote:
Hi,
I am using SQL Server as a Database in my ASP.NET application. Can
anyone
tell me, how to store my connection string in web.config file and
access it
from my application.
It is very urgent please..
-- Perin


Nov 18 '05 #7

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

Similar topics

12
7691
by: jacob nikom | last post by:
Hi, I would like to store XML files in MySQL. What is the best solution: 1. Convert it to string and store it as CLOB/text 2. Serialize it and store as byte array 3. Flatten it out and create...
4
2682
by: Steve | last post by:
C# I am trying to store my connection settings in a config file, mainly so I can easily change the db that the app connects to without doing a release of code. I have the config file sorted...
4
2431
by: Alberto | last post by:
I have to store a connection string to a data base but I don't know where. Where do you prefer store it, in a text file or in a xml file? Thank you.
2
2267
by: Chris Dunaway | last post by:
I have a web service that references a data class library which performs SQL Server access. Since the web service is also a class library, there is no App.Config, only Web.config. Is Web.Config...
5
1288
by: Simon Harvey | last post by:
Hi everyone, As I understand it, storing an applications SQL Server connection string in the web.config file is a security risk. I'm wondering then, what the simplest solution is to this...
4
2409
by: Leo Muller | last post by:
I am about to deploy my first ASP.NET project. But, I was used to store my variables, such as connection strings, in the global.asa file, in an application. However, these variables are different...
5
4487
by: Guadala Harry | last post by:
What are my options for *securely* storing/retrieving the ID and password used by an ASP.NET application for accessing a SQL Server (using SQL Server authentication)? Please note that this ID and...
8
2550
by: Merk | last post by:
I'm looking for a safe and maintainable way to store connection string info (connecting to SQL Server 2005 from .NET 2.0 Windows Forms client app); things like server name or IP address and...
2
1610
by: Man T | last post by:
I just wonder what is/are your practices to build the SQL Server connection string? Use object to build the connection string from config file or somewhere?
0
7040
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
7041
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
7080
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
5331
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
4772
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
4478
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1299
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
561
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.