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:

NorthwindEmployees.aspx
NorthwindEmployees.aspx.cs

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 *****
    [WebMethod]
    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>();
                con.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        employees.Add(new Employees
                        {
                            EmployeeID = sdr["EmployeeID"].ToString(),
                            LastName = sdr["LastName"].ToString(),
                            FirstName = sdr["FirstName"].ToString(),
                        });
                    }
                }
                con.Close();
                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:

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

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
and
instead put this connection string in the machine.config of these directories on my server.

C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config

C:\Windows\Microsoft.NET\Framework64\v2.0.50727\CONFIG\machine.config

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config


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;
LVL 1
maqskywalkerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Miguel OzSoftware EngineerCommented:
You can use:
Configuration machineConfig = ConfigurationManager.OpenMachineConfiguration();

Open in new window

see this link for more info.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
maqskywalkerAuthor Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.