ConnectionStrings in web.config configuration file - Part 3

Sharing buttons:

hello welcome to prism technologies I am

venket this is part three of radial or

net video series in this session we'll

learn about storing connection strings

in a configuration file reading the

connection strings from these

configuration files such as web config

and AB dot config files what are the

disadvantages of storing connection

strings in application code advantages

of storing connections shrink in

configuration files such as web config

and applet config before continuing with

the session I strongly recommend to

watch parts 1 & 2 of this video series

now in part 2 of this video series we

have seen you know to connect to a

sequel server in our database we create

the connection string first which is

nothing but a string of name value pairs

and then we instantiate the sequel

connection object using the connection

string and then we prepare the sequel

command open the connection execute the

commands have those results as the

datasource property of the gridview and

then data bind and we are not closing

the connection here because the using

statement will force the connection to

be automatically closed we don't have to

call that manually okay so this using

statement ensures that the connection is

always closed irrespective of whether

there is an exception or not ok but then

this code is properly latent but there

is one problem with this code that is

hard coding the connection string in the

application code this is not suggested

and it's not a good practice why let's

understand the problems of storing

connection strings in code ok the first

problem is you know tomorrow if I have

to point my application code to another

database then I need to change this

connection string so when you change

application code obviously you have to

rebuild the application and redeploy

that so obviously building and

redeploying is going to consume time ok

so that's one problem every time you

have to change your application database

then you will have to rebuild and

redeploy you if you're wondering what is

the reason why do we want to change the

database to which my application is

pointing in real time

you know constantly there is a need for

the know you know if I am testing the

application the application code will be

pointing to a testing database on the

same lines if there is you ad testing

going on then the application code needs

to point to you a T database and when I

move the application code to production

environment then the application code

has to point to the production database

or if we are just developing it then the

application code has to point to a

development database so definitely there

is a need for the application code you

know to point to different databases and

during that time we don't want to be

changing the application code because

that requires a rebuild and redeployment

which is time-consuming and there is

another major problem another problem is

let's say in my application I have you

know maybe 100 or 200 web forms

depending on the complexity of the

application you have so many web forms

now if I have to obviously if if I have

the connection string hard-coded on all

the web forms like this then we need to

change that on all those hundred web

forms which is definitely error-prone if

you forget to do that on a few of the

files just imagine you know some of the

files will be pointing to one bit one

database and another set of files are

pointing to another database definitely

error prone that's why you know it's a

good practice to store these connection

strings in a configuration file so if I

have an web application then

this web application has got web

config the configuration file and if I

want to store the connection string in

web config file you know in the

configuration section look at this we

already have this connection strings

element so within the connection strings

element I can add a meaningful you know

a connection string so give a meaningful

name let's call this you know DB CS for

shortcut database connection string and

within your connection string all you

need to do is you need to specify the

data source the database name as usual

the same connection string so I'm going

to copy this

and then paste that in our connection

string property here and then provide a

name system dot data dot sequels lines

since we are interacting with sequel

server the provider name is going to be

system dot data dot sequel of line okay

so we now have the connection string

stored in web config file so we can get

rid of this particular line here okay so

we stored it in the configuration file

but we need to tell our application to

read from that connection string so how

do I tell my application to do that to

do that you know in dotnet framework we

have a class called configuration

manager look at the name the name is

also meaning meaningful configuration

manager so this class is going to manage

the configuration for my application so

we can use that class and then read the

connection string from web config file

okay so configuration manager let's use

that class and if I try to type it I

will not find it here

that's because configuration manager

class is present in a different name

namespace called system dot

configuration look at this even the

namespace is meaningful now I want to

deal with the configuration from web

config file so i am using configuration

manager class which is present in system

to configuration namespace so import

that namespace using system dot

configuration so within that we have

this configuration manager so

configuration manager dot there is a

property called connection strings okay

now if you just use this connection

strings it's going to return all the

properties all the connection strings

because it's possible to have multiple

connection strings in web config file

but I don't want all the connection

strings I just want a connection string

that has got DB CSS the connection

string name so what I'm going to do is

I'm going to use that so you can here

either specify an integer index if I use

0 I will automatically get the first

connection string from this section

since I have only one I will get this

but then it's it's good practice to

actually use then

so that you know which connection string

we are referring to so let's call DBC s

is our connection string name and then

dot we want the connection string so I

use that property so store that in a

variable string connection string is

equal to that one and then what we do

simply use that as a parameter to your

sequel connection object that's it we

are done we have stored our connection

string and web config file and we have

seen how to actually read it from the

web config file so if we press ctrl fi

what's going to happen there I mean in

the application code behavior it's not

going to change in any way it's just

that we have written this application

you know in a good way we are storing

your connection strings in web config

file so tomorrow even if you have you

know 100 different web forms here and

then all the wave forms if you are using

database connections then obviously you

are creating this connection object but

still since you are reading from one

location to more if you have to change

your database all you have to do is

change this web.config file connection

string I change the name of the server

and the database that's it all pages are

now pointing to a different server and

the different database and this is not

error-prone you have only one place to

change and you don't have to redeploy

your application because it's a web

config file so you don't have to

recompile all right so if it's an web application then we have got

web config file but if it is a Windows

or a console application then they don't

have web config file it's AB dot config

file I mean it's the same logic if it's

an app dot config file

all you do is you specify you know the

connection string in the same way and

you use the configuration manager class

to read from that let's actually open

let's close the solution and then open a

Windows application I already have a

Windows application that we have been

working with before so let me open this

Windows application which again talks to

the same database and then pulls that


look at this even in this windows

application we have this connection

string hard-coded we don't want to do

that we want to store that in a

configuration file and to do that first

of all we need to have a configuration

file a web application gets web config

by default but for Windows application

you don't get that you have to add that

and to do the right click on the

application name add and then a new item

and you can specify application

configuration file look at this and the

moment I select it look at the name app

config click add the configuration file

gets added and all you have to do is

specify the connection string so add a

m-- you know instead of opening typing

all that I'm going to copy that from our

a do demo ed your demo you should have

web config file so let's copy it from

there it's just going to open up now

actually we should have opened that with

a notepad which should have been much

faster okay here we go so let me copy

that connection strings node from there

I'm going to paste that within our

Windows application ok so we have the

connection string here all we have to do

in our code behind file is read that ok

so I don't want to hard code it like

this instead I want to read it from

using the configuration manager class

configuration manager is present in

system dot configuration and let's use

the configuration manager plus as usual

so configuration look at this I don't

find configuration manager in spite of

importing the system dot configuration

namespace that's because the assembly

reference might not be there so you need

to add system dot configuration assembly

reference and to do that right click on

you know that's only or will get

automatically referenced if it is an web application but if it is a

Windows application it doesn't get

referenced but you can out always had

that so ant reference

and then from the dotnet tab we will

select the system dot configuration

assembly so scroll down to system

configuration and then add that so I

have system configuration click OK that

assembly should get added now if I type

configuration manager you should see the

class so in the same way configuration

manager dot connection strings of DB CS

is the name of the connection string dot

connection string and I want to store

that in a variable string CS or if you

don't want to store it in a variable you

can simply use this property directly in

the constructor of sequel connection

class and we are done that's it now when

once we run this we are now reading the

configuration information from web

config file we don't have it hard-coded

here even console application has app

config file so if if your console

application is interacting with database

then use you know app dot config file to

store your connection strings on this

slide you can find resources for

c-sharp and sequel server interview

questions that's it for today thank you

for listening have a great day