0

I am working to discover how to work with a layered architecture, and I am running into a problem.

This is the code from my data layer:

using System.Data;
using System.Data.SqlClient;

namespace Datalaag_Class_Libary
{
    public class Datalaag_Connectie_Class
    {
        public string ConnectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
        SqlConnection con = new SqlConnection();
        DataTable dt = new DataTable();
        public DataTable Read()
        {
            con.ConnectionString = ConnectionString;
            if (ConnectionState.Closed == con.State)
                con.Open();
            SqlCommand cmd = new SqlCommand("select * from demo_wedstrijdschema" , con);
            try
            {
                SqlDataReader rd = cmd.ExecuteReader();
                dt.Load(rd);
                return dt;
            }
            catch
            {
                throw;
            }
        }
    }

    }

This is the code form the business layer:

using System.Data;
using Datalaag_Class_Libary;

namespace Businesslaag_Class_Libary
{
    public class Busines_laag_class
    {
        public DataTable GetInfo()
        {
            try
            {
                Datalaag_Connectie_Class objdal = new Datalaag_Connectie_Class();
                return objdal.Read();
            }
            catch
            {
                throw;
            }
        }

    }
}

And this is the code from the user interface layer:

using System;
using System.Windows.Forms;
using Businesslaag_Class_Libary;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                Busines_laag_class test = new Busines_laag_class();
                this.dataGridView1.DataSource = test.GetInfo();
            }
            catch
            {
                MessageBox.Show("error");
            }
        }
    }
}

The program can run (not as it should run), but in the output screen the following error occurs: Exception thrown: 'System.IO.FileNotFoundException' in Businesslaag_Class_Libary.dll This is my full output screen: Errorscreen The asked exception output: ex.tostring message

Dont know what I have done wrong in the code. Can anyone help? Thanks in advance!

Josse
  • 55
  • 5

2 Answers2

0

I think one of your assembly files is missed, which is System Data SqlClient. Try to add this package to your data layer with this command or directly by yourself.

dotnet add package system.data.sqlclient
Ali Kianoor
  • 1,167
  • 9
  • 18
  • I checked for this in my ManageGetNU packeges, but I have System.Data.SqlClient installed. – Josse May 11 '20 at 18:26
  • so try to add it in your form or business layer, because the problem is the system can not recognize your system.data.sqlclient reference correctly! – Ali Kianoor May 11 '20 at 18:29
0

You may need to add a reference to System.Data.SqlClient from your WindowsFormsApp1 project. Try adding the same version as is used from the data layer project.

It seems the project of data layer did not copy the assembly System.Data.SqlClient.dll to output folder and there is no 'Copy local' option anymore.

Martin Staufcik
  • 8,295
  • 4
  • 44
  • 63