Unable to cast object of type 'Microsoft.Data.SqlClient.SqlConnection' to type 'System.Data.SqlClient.SqlConnection

Polachan Paily 226 Reputation points
2021-06-10T08:39:58.16+00:00

I was using mv project in .Netframe work 2.0 and then now I have changed into .netframework 3.1 , Now I am getting the error as ''Unable to cast object of type 'Microsoft.Data.SqlClient.SqlConnection' to type 'System.Data.SqlClient.SqlConnection' in the various places having the code as given below. How can I change the code to work with .netframework 3.1 , Please help

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
//using Microsoft.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

 var conn = (SqlConnection)_db.Database.GetDbConnection();
                DataTable dtMissingDates = new DataTable();
            SqlParameter parUserId = new SqlParameter("@UserId", userId);
            SqlParameter parDepots = new SqlParameter("@DepotNo", depono);
                SqlParameter parDepartment = new SqlParameter("@DepartmentID", department);
                SqlParameter parDate = new SqlParameter("@DateTo",attdate);
            SqlHelper.FillDatatable(conn
                                            , CommandType.StoredProcedure
                                            , StoredProcedureConstants.GET_DEPOT_MISSING_ATT_POPUP
                                            , dtMissingDates
                                            , MTSGOConstants.DEFAULT_COMMAND_TIME_OUT
                                            , parUserId
                                            , parDepots
                                            , parDepartment
                                            ,parDate);
Developer technologies ASP.NET ASP.NET Core
{count} votes

1 answer

Sort by: Most helpful
  1. Duane Arnold 3,216 Reputation points
    2021-06-10T11:10:09.67+00:00

    How is it that you have an EF Core namespace in the class and yet you are doing something with a datatable? It doesn't seem to be optimal programming.

    https://dzone.com/articles/reasons-move-datatables

    You can create a custom class a data container class. You can execute the sproc using EF using the custom class returning a list. And you can give the parameters too to the sproc.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.