Web.Config Connection String Settings in .NET 2.0

We have been doing a lot of asp.net 2.0 development lately.  Here is how we are setting up the web.config file to hold our connection settings.  First, set your web.config file Connection String settings as follows:

<add name="YourConnectionString" connectionString="Data Source=YourSQLServer;Initial Catalog=YourDatabase;User ID=YourUserID;Password=YourPassword" providerName="System.Data.SqlClient" />

In your data access layer classes set the sqlconnection object to the web.config connection string thru the constructor. For VB.NET that is the class’s New() function.  In C# it is the function named the same as the class.  Below are both examples or download the C# version or VB.NET version directly.

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.UI

Public Class DataAccess

Dim conn As New SqlConnection
Dim sql As String
Dim objSql As SqlCommand
Dim MyDataReader As SqlDataReader

Public Sub New()
If ConfigurationManager.ConnectionStrings("YourConnectionString") Is Nothing OrElse _
ConfigurationManager.ConnectionStrings("YourConnectionString").ConnectionString.Trim() = "" Then
Throw New Exception("Connection Error")
conn.ConnectionString = ConfigurationManager.ConnectionStrings("YourConnectionString").ConnectionString
End If

End Sub

Public Function GetMSRP(ByVal productid As Int32) As String

Dim strUnitPrice As String = Nothing
sql = "SELECT UnitPrice FROM Products WHERE productid = @productid"
objSql = New SqlCommand(sql, conn)
‘Add Parameters
objSql.Parameters.AddWithValue("@productid", productid)

‘Open Connection
‘Execute DataReader
MyDataReader = objSql.ExecuteReader
‘Store Values in String Variables
While (MyDataReader.Read())
strUnitPrice = MyDataReader.Item("UnitPrice")
End While
‘Close Connection
Catch ex As Exception
End Try
Return strUnitPrice
End Function

End Class

################### c# version

using Microsoft.VisualBasic;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
public class DataAccess {
    private SqlConnection conn = new SqlConnection();
    private string sql;
    private SqlCommand objSql;
    private SqlDataReader MyDataReader;
    public DataAccess() {
        if (((ConfigurationManager.ConnectionStrings("YourConnectionString") == null)
                    || _)) {
            ConfigurationManager.ConnectionStrings("YourConnectionString").ConnectionString.Trim() = "";
            throw new Exception("Connection Error");
        else {
            conn.ConnectionString = ConfigurationManager.ConnectionStrings("YourConnectionString").ConnectionString;
    public string GetMSRP(Int32 productid) {
        string strUnitPrice = null;
        sql = "SELECT UnitPrice FROM Products WHERE productid = @productid";
        objSql = new SqlCommand(sql, conn);
        // Add Parameters
        objSql.Parameters.AddWithValue("@productid", productid);
        try {
            // Open Connection
            // Execute DataReader
            MyDataReader = objSql.ExecuteReader;
            // Store Values in String Variables
            while (MyDataReader.Read()) {
                strUnitPrice = MyDataReader.Item["UnitPrice"];
            // Close Connection
        catch (Exception ex) {
            // TODO HANDLE EX
        return strUnitPrice;


A .NET, PHP, Marketing Guru authority, at least I hope I am. Reach me at tboland@gmail.com

12 thoughts on “Web.Config Connection String Settings in .NET 2.0

  1. your sample is good but leaves out few stuff.

    Go to the MyProject page and select References. Click “Add” and go to the .NET tab and select System.Configuration.

    Also, class has following imports
    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Configuration

  2. I am sorry. I could not get your code to work. Keep getting connection could be opened or something like that.

  3. There are couple of problems i think with this apporahc
    1. userid and password int config files.

    2. Moving from Development to Qa to prouction someone needs to manually change the values for production system this would be a problem.

    And usually the development group do have the passwords for production databases… is there way to a create a data source and externally and refer to the datasource by name in the config file?

  4. So there were 3 tomatoes OK, a papa tomato, a mama tomato and a baby tomato. One day the 3 tomatoes were walking down the street and baby tomato kept falling behind. After a couple of times, the mama tomato gets upset and smashes the baby tomato and says “Catch Up”.

    Get it?

Leave a Reply

Your email address will not be published. Required fields are marked *