referencing a connection string found in machine.config of server

I'm using ASP.NET Web Forms , C# and Sql server

I'm using the Employees table from Northwind sql server test database.

I created an empty C# web site in Visual Studio 2013

So in my website I only have 1 page and it's code behind file which are these:


I also have the Web.Config file.

This is the code for my C# file: NorthwindEmployees.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Data.SqlClient;
using System.Configuration;

public partial class NorthwindEmployees : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)

    //***** Employees List *****
    public static List<Employees> GetEmployees()
        string nwemployeesconn = ConfigurationManager.ConnectionStrings["nwemployeesconn"].ConnectionString;
        using (SqlConnection con = new SqlConnection(nwemployeesconn))
            using (SqlCommand cmd = new SqlCommand("SELECT EmployeeID LastName, FirstName FROM [Northwind].[dbo].[Employees]"))
                cmd.Connection = con;
                List<Employees> employees = new List<Employees>();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                    while (sdr.Read())
                        employees.Add(new Employees
                            EmployeeID = sdr["EmployeeID"].ToString(),
                            LastName = sdr["LastName"].ToString(),
                            FirstName = sdr["FirstName"].ToString(),
                return employees;
    //***** Employees List *****

//***** Employees Class *****
public class Employees
    public string EmployeeID { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
//***** Employees Class *****

Open in new window

In my Web.Config file I have this connection string:

<add name="nwemployeesconn" connectionString="Data Source=testsqlserver1; Initial Catalog=Northwind;User ID=johnuser;Password=usertestpassword1" providerName="System.Data.SqlClient" />

Open in new window

The  GetEmployees function inside my file NorthwindEmployees.aspx.cs calls the connection string called nwemployeesconn which is found in my Web.Config file.
It then runs the Sql query command to get the EmployeeID, LastName, FirstName columns from the Northwind Employees table and created a list of those employees.

This function works fine and this connection string works fine and my data is good.

If I get this connection string

<add name="nwemployeesconn" connectionString="Data Source=testsqlserver1; Initial Catalog=Northwind;User ID=johnuser;Password=usertestpassword1" providerName="System.Data.SqlClient" />

found in my Web.Config
instead put this connection string in the machine.config of these directories on my server.





How would I call that connection string that is now located in the machine.config, from inside my GetEmployees() function inside of this file NorthwindEmployees.aspx.cs ?

Right now I'm calling the connection string that is inside of my Web.Config with this linke of cod:

string nwemployeesconn = ConfigurationManager.ConnectionStrings["nwemployeesconn"].ConnectionString;
Who is Participating?
Miguel OzSoftware EngineerCommented:
You can use:
Configuration machineConfig = ConfigurationManager.OpenMachineConfiguration();

Open in new window

see this link for more info.
maqskywalkerAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.