Question

I was doing a basic example of 3 tier architecture in c#.I created two dll's for data and business layer.Also I am using data layer dll in business layer code.And,business dll and data access dll in presentation layer(which is a winform application).Now,when the presentation layer code is being executed,an exception is coming which says :

Database 'D:\11feb\practice\3tier\PresentationLayer\PresentationLayer\bin\Debug\Data.mdf' do not exists.

I had created my database Data.mdf in data layer. I copied the database files to the location mentioned in exception and the application successfully got executed.But I want the database to be accessed from my data layer.

Data Layer Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DataAccessLayer
{
    public class DataAccess
    {
        public DataTable dataRead()
        {

            SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Data.mdf;Database=Data;Integrated Security=True;User Instance=True");
            DataTable dt = new DataTable();
            con.Open();
            SqlCommand cmd = new SqlCommand("select ID,Name from datatable", con);
            try
            {
                SqlDataReader rd = cmd.ExecuteReader();
                dt.Load(rd);
                return dt;
                }
                catch
                {
                    throw;
                }

            }
        }
    }

Business Layer Code:

      using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using DataAccessLayer;
        using System.Data;

        namespace BusinessLogicLayer
        {
            public class BusinessLogic
            {
                DataAccess dataAccess = new DataAccess();

                public DataTable getPersons() 
                {
                    try
                    {
                        return dataAccess.dataRead();
                    }
                    catch { throw; }
                }
            }
        }

Presentation Layer Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLogicLayer;

namespace PresentationLayer
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                BusinessLogic BusinessLogic = new BusinessLogic();
                this.dataGridView1.DataSource = BusinessLogic.getPersons();
            }
            catch
            {
                MessageBox.Show("Error Occurred");
            }
        }
    }
}
Was it helpful?

Solution

The problem is that you have added Data.mdf in the solution but when the application runs, it tries to find out the mdf file in the bin directory. Click on Data.mdf file in the solution. Go to its properties ( By Pressing F4) and then look out for the property "Copy to the Output Directory" and then change the value to Copy Always.

Also check out your connection string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top