question

PolachanPaily-2805 avatar image
0 Votes"
PolachanPaily-2805 asked DuaneArnold-0443 answered

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

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);
dotnet-aspnet-core-mvc
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

According to the migration documentation .NET 3.X uses Microsoft.Data.SqlClient. The recommended solution is to upgrade your source code to use Microsoft.Data.SqlClient. Otherwise, download Microsoft.Data.SqlClient from NuGet.

My best guess is the SqHelper.Filldatatable defines a System.Data.SqlClient.SqlConnection by you pass a Microsoft.Data.SqlClient.SqlConnection. Unfortunately, you did not share any relevant code which makes providing a solution difficult. Review your code.


0 Votes 0 ·

1 Answer

DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.