keesjanbertus
keesjanbertus

Reputation: 402

Can't add reference to System.Data.*

My c# solution had only one project at first where I had UI, Business, DAL in that project. I could use System.Data.SQLClientReader without problems. Now that I seperated it into 3 projects: (UI, Business, DAL) where Business and DAL are class libraries.

When I hover over for instance SqlConnection in the DAL and go to Quick fixes, I suddenly have to install it using NuGet. After doing this and visual studio not showing any errors in the background I start the program. I then receive this error in the Business layer calling a crud class in the DAL:

System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Data.SqlClient, Version=4.5.0.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.'

Crud class:

using System;
using System.Data.SqlClient;

namespace BornToMove.DAL {
    public class Crud {

        public Crud() { }

        public SqlDataReader Read(SqlCommand cmd) {
            return cmd.ExecuteReader();
        }

        public long Create(SqlCommand cmd) {
            return Convert.ToInt64(cmd.ExecuteScalar());
        }
    }
}

DbMove class (only showing one method):

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

namespace BornToMove.DAL {
    public class DbMove : IDbMove {

        private Crud Crud { get; set; }

        public DbMove(Crud crud) {
            this.Crud = crud;
        }

        public Move GetMove(long id) {
            SqlConnection conn = null;
            try {
                conn = new MSSqlConn().GetConn();
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT * FROM Move WHERE id=@id";
                SqlParameter idParam = new SqlParameter("@id", System.Data.SqlDbType.Int) {
                    Value = id
                };
                cmd.Parameters.Add(idParam);
                cmd.Prepare();
                SqlDataReader reader = this.Crud.Read(cmd);
                Move move = null;
                if (reader.Read()) {
                    move = new Move {
                        Id = (long)reader.GetInt32(0),
                        MoveName = reader.GetString(1),
                        MoveDescription = reader.GetString(2),
                        Rating = reader.GetInt32(3),
                        SweatRate = reader.GetInt32(4)
                    };
                }
                return move;
            } catch {
                return null;
            } finally {
                conn.Close();
            }
        }

Why do I suddenly have to install it using NuGet? And why does this not even work?

Upvotes: 1

Views: 4051

Answers (3)

user16603211
user16603211

Reputation:

I had a similar issue in an application which referenced a DLL I had built using the NuGet System.Data.SQLClient - unless the same NuGet package was also installed in the application there was an error as described above. Once I installed the NuGet SQLClient package in the application project all went perfectly.

I think your problem is that your Business layer, by calling the DAL, caused an error as the NuGet SQLClient package was not installed in either the business layer or the entire application project.

I hope that's useful,

Dermot

Upvotes: 1

keesjanbertus
keesjanbertus

Reputation: 402

I fixed this by making a new solution and instead of making the DAL and Business layers of type class library (.NET Standard) I used class library(.NET Framework). I was then able to reference the dll without any problems

Upvotes: 2

Akshay G
Akshay G

Reputation: 2280

The problem is with the version of System.Data.SqlClient dll in different projects. You should either use same verison in multiple projects or redirect to newer version by mentioning the dependentAssembly in app.config

In your case the System.Data.SqlClient dll should e referenced only in DAL and removed from Business and UI because it is of no use there.

System.Data.SqlClient namespace is available in Sytem.Data dll

Upvotes: 1

Related Questions