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(DataObje ctMethodTy pe.Select, true)]
public List<Employee> GetEmployees()
{
UtilityClass uC = new UtilityClass();
string conString = string.Empty;
foreach (ConnectionStringSettings s in ConfigurationManager.Conne ctionStrin gs)
{
Console.WriteLine(s.ToStri ng());
}
conString = ConfigurationManager.Conne ctionStrin gs["Northw ind"].Conn ectionStri ng;
SqlConnection cnNwind = new SqlConnection(conString);
cnNwind.Open();
SqlCommand cmd = new SqlCommand("GetAllEmployee s", 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["TitleOfCou rtesy"];
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???
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(DataObje
public List<Employee> GetEmployees()
{
UtilityClass uC = new UtilityClass();
string conString = string.Empty;
foreach (ConnectionStringSettings s in ConfigurationManager.Conne
{
Console.WriteLine(s.ToStri
}
conString = ConfigurationManager.Conne
SqlConnection cnNwind = new SqlConnection(conString);
cnNwind.Open();
SqlCommand cmd = new SqlCommand("GetAllEmployee
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["TitleOfCou
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???
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.