[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 448
  • Last Modified:

Connection String in DAL

Hi,
I'm developping an enterprise app and have defined a data access layer in which I have my entities classes and the data access classes. I've defined a the connection string in the data access layer application to a the local northwind database.  ee the example code for the employee entity and data access classes:

public class Employee
    {
        private int employeeID;
        private string firstName;
        private string lastName;
        //private string title;
        private string titleOfCourtesy;
        //private DateTime birthDate;

        public int EmployeeID
        {
            get { return employeeID; }
            set { employeeID = value; }
        }

        public string FirstName
        {
            get { return firstName; }
            set { firstName = value; }
        }

        public string LastName
        {
            get { return lastName; }
            set { lastName = value; }
        }

        public string TitleOfCourtesy
        {
            get { return titleOfCourtesy; }
            set { titleOfCourtesy = value; }
        }
    }

//data access class
[DataObjectMethod(DataObjectMethodType.Select, true)]
        public List<Employee> GetEmployees()
        {
            UtilityClass uC = new UtilityClass();
            string conString = string.Empty;
            foreach (ConnectionStringSettings  s in ConfigurationManager.ConnectionStrings)
            {
                Console.WriteLine(s.ToString());

            }
           
            conString = ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;

            SqlConnection cnNwind = new SqlConnection(conString);
            cnNwind.Open();
            SqlCommand cmd = new SqlCommand("GetAllEmployees", cnNwind);

            List<Employee> employees = new List<Employee>();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Employee emp = new Employee();
                emp.EmployeeID = (int)reader["EmployeeID"];
                emp.FirstName = (string)reader["FirstName"];
                emp.LastName = (string)reader["LastName"];
             
                emp.TitleOfCourtesy = (string)reader["TitleOfCourtesy"];
               
                employees.Add(emp);
            }

            return employees;
        }


    }

In my client application, when I instanciate the Employee class, it can not find the connection string I've defined into the configuration file.

It looks like the employee class is looking for the connection string in the client application configuration file instead of in the data access layer aplication configuration file even though the employee class is defined in the data access layer class.

Any idea???
0
racineconde
Asked:
racineconde
  • 2
1 Solution
 
lazyberezovskyCommented:
Configuration is taken from .exe file config (App.config), not from dll config file. You should add your DAL configuration settings to application configuration file.
0
 
lazyberezovskyCommented:
0
 
arundhajCommented:
Your question has the answer.

Your DAL application is a DLL and it is being reference from the client application, right? In that case, your client application should also have the key for "Northwind" in the configuration file.
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now