Hello I hope someone can help me. I'm creating a solution wich holds a program and a website in a 3 layer architecture. Whilst trying to access the data from the database things get dodgy. I'll try to explain my problem.
I use the following projects:
1. InternalProgram: the application
2. Website: the website ^^
3. BlCommon: wich holds the business logic common to both the site and the internal program
4. DAL: the data acces layer wich holds the database and classes to acces the information wich it holds.
5. Runner: A project to "translate" the dataobjects from the business logic and the DAL database layer. Simply to avoidi circular dependancy
I can show the products from my database in a form inside my internalprogram layer with no issues. The chain I use is the same when I try to acces the same product data in the website layer. However the website layer returns the following error:
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code
Additional information: An attempt to attach an auto-named database for file C:\<myPathToDatabase>\Database.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
My connection string is relative:
Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True
My code requests a list of products:
public static List<ProductDO> GetAllProducts()
{
using (SqlConnection connection = getConnection())
{
using (SqlCommand command = new SqlCommand("select id, afbeelding, naam, omschrijving, prijs from Producten", connection))
{
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
List<ProductDO> productsDO = new List<ProductDO>();
while (reader.Read())
{
ProductDO productDO = new ProductDO(
Convert.ToInt32(reader["id"]),
reader["afbeelding"].ToString(),
reader["naam"].ToString(),
reader["omschrijving"].ToString(),
Convert.ToDouble(reader["prijs"]));
productsDO.Add(productDO);
}
return productsDO;
}
}
}
}
That list is sent to the the businessLayer wich converts it to a list of products after converting it from productDO
The code to convert from productDO to product:
public static Product convertFromDo(ProductDO productDO)
{
Product product = new Product(productDO.Id, productDO.AfbeeldingsLocatie, productDO.Naam, productDO.Omschrijving, productDO.Prijs);
return product;
}
the code to get the list of products
public static List<Product> GetAllProducts()
{
List<Product> allProducts = new List<Product>();
List<ProductDO> allProductsDO = DataAccess.GetAllProducts();
foreach (ProductDO pdo in allProductsDO)
{
allProducts.Add(convertFromDo(pdo));
}
return allProducts;
}
the code to display the list of products in my winform: this code works fine
List<Product> allProducts = Product.GetAllProducts();
foreach (Product p in allProducts)
{
labelAllProducts.Text += p.Naam + Environment.NewLine;
}
however when i do the same thing in my website layer:
List<Product> allProducts = Product.GetAllProducts();
foreach (Product p in allProducts)
{
labelAllProducts.Text += p.Naam + "<br />";
}
it crashes on the dataacces layer line: connection.open();
Can someone plz help me out?
Thx