In a website created using ASP .net there is a file called web.config . This file holds information about the website’s configuration.
From web.config’s Wikipedia entry:
Web.config is the main settings and configuration file for an ASP.NET web application. The file is an XML document that defines configuration information regarding the web application. The web.config file contains information that control module loading, security configuration, session state configuration, and application language and compilation settings.
One setting that is useful is the connection string setting. If you are connected to a database you can store all the connection string variables in the web.config file and reference them from pages within the website.
This is a good thing since there is only one place to edit the connection string. So if something changes you only have to look once for the string.
But how do you retrieve the string from the config file and use it in a page?
Let’s say you are creating a connection to an SQL database in the VB code behind. Instead of writing out the connection string each time you write some code to connect to the database, try using the ConfigurationManager.ConnectionStrings() method.
- SqlConnection = the connection object. It takes a connection string as it’s parameter.
Example in VB w/ color enhancements!:
Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings(“name of connection string from web.config“).ConnectionString)
‘in here will be the rest of your code
End Using
See, now if something changes you can go to one place: the web.config file, instead of having to change code in multiple locations.