Link to home
Start Free TrialLog in
Avatar of racineconde
racineconde

asked on

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???
Avatar of lazyberezovsky
lazyberezovsky
Flag of Belarus image

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.
ASKER CERTIFIED SOLUTION
Avatar of lazyberezovsky
lazyberezovsky
Flag of Belarus image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.