[Solved] ASP.Net Core: system.data.sqlclient is not supported on this platform

kid.live
 
on Jan 18, 2023 11:31 PM
565 Views

Hello Experts, Hope You Are Doing Well. 

I am Trying To Create a WebApi. However My Approach Is Little Different Because Of My Manager Ask Me To Do Like That. My Whole Project Is In .Net Core FrameWork 6. 1. Main WebAPI 2. Services (Different Project Same FrameWork) 3. Models (Different Project Same FrameWork)

Below Code Is No 2. DB Connecion Service Project. The Whole Api Is Interlinked. And References Is Added. However. WebApi Is Running Fine. But When I Try To Login To Get The Token. 

Error Comes "system.platformnotsupportedexception 'system.data.sqlclient is not supported on this platform.'

Could Anyone Please Help.

using Core.Mbm.Hrm.Model;
using System.Data;
using System.Data.SqlClient;

namespace Core.Mbm.Hrm.Services
{
    public class HrLogin
    {

        public static class GetConString
        {
            public static string ConString()
            {
                var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
                var config = builder.Build();
                string constring = config.GetConnectionString("DbConn");
                return constring;
            }
        }


        public string CheckUserPassword(LoginModel obj)
        {
            //using (SqlConnection dbconn = new SqlConnection(DbConn.ConnectionString))
            using (SqlConnection dbconn = new SqlConnection(GetConString.ConString()))
            {
                SqlCommand COM = new SqlCommand("WP_USER_LOGIN_CHECK", dbconn);
                COM.CommandType = CommandType.StoredProcedure;
                SqlDataReader RD;

                try
                {
                    COM.Parameters.AddWithValue("@EMPNO", obj.UserId);
                    COM.Parameters.AddWithValue("@PASSWORD", obj.Password);

                    if (dbconn.State == System.Data.ConnectionState.Closed)
                    {
                        dbconn.Open();
                    }
                    RD = COM.ExecuteReader();
                    if (RD.HasRows)
                    {
                        RD.Read();
                        Int16 _VALID = 0;
                        _VALID = Int16.Parse(RD["VL"].ToString());
                        if (_VALID == 1)   //&& obj.HostDetails == RD["REG_HOSTID"].ToString())
                        {
                            return "1";
                        }
                        else
                        {
                            return "0";
                        }
                    }
                }
                catch (SqlException ex)
                {
                    throw;
                }
                finally
                {
                    COM.Dispose();
                    dbconn.Close();
                }
            }
            return "0";
        }
    }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jan 18, 2023 11:31 PM
on Jan 19, 2023 01:14 AM

Downgrade Microsoft.Data.SqlClient to 2.0.1. And install the Microsoft.Data.SqlClient.SNI.Runtime v2.0.1 nuget package.

Then copy the below dll's to bin folder.

packages\microsoft.data.sqlclient.sni.runtime\2.0.1\runtimes\win-x64\native\Microsoft.Data.SqlClient.SNI.dll

packages\microsoft.data.sqlclient\2.0.1\runtimes\win\lib\netcoreapp3.1\Microsoft.Data.SqlClient.dll

Reference

https://stackoverflow.com/questions/71022502/net-6-system-platformnotsupportedexception-microsoft-data-sqlclient-is-not-sup