The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlParameter objects in Microsoft.Data.SqlClient

1

I'm trying to use one of these namespaces

//using Microsoft.Data.SqlClient;
using System.Data.SqlClient;

My model

public class Get_Data_Scholar{
    [Key]
    public Int32 ID_Transcript { get; set; }
    public string Year_Semester { get; set; }
    public string Period { get; set; }
    public string Status { get; set; }
}

My controller

        public JsonResult Get_GPA_Tuition (Int32 id)
        {
            var ID_NCS = new SqlParameter("@ID_NCS", id);
            List<Get_data> get_Data = new List<Models.Get_Data_Scholar>();
            get_Data = _applicationDbContext.Get_Data_Scholar.FromSql(
                            "EXEC [dbo].[Get_Data_Scholar]  @ID"

                            , id
                            ).ToList();

            return Json(get_Data );
        }

When I'm using System.Data.SqlClient, everything works well. But when I change different namespace to Microsoft.Data.SqlClient, it produce error message like this:

System.InvalidCastException HResult=0x80004002 Message=The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlParameter objects. Source=System.Data.SqlClient
StackTrace: at System.Data.SqlClient.SqlParameterCollection.ValidateType(Object value) at System.Data.SqlClient.SqlParameterCollection.Add(Object value) at Microsoft.EntityFrameworkCore.Storage.Internal.DynamicRelationalParameter.AddDbParameter(DbCommand command, Object value) at Microsoft.EntityFrameworkCore.Storage.Internal.CompositeRelationalParameter.AddDbParameter(DbCommand command, Object value) at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalParameterBase.AddDbParameter(DbCommand command, IReadOnlyDictionary2 parameterValues) at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.CreateCommand(IRelationalConnection connection, IReadOnlyDictionary2 parameterValues) at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary2 parameterValues) at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary2 parameterValues) at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1.Enumerator.BufferlessMoveNext(Boolean buffer) at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func3 operation, Func3 verifySucceeded) at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1.Enumerator.MoveNext() at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.<_TrackEntities>d__172.MoveNext() at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor1.EnumeratorExceptionInterceptor.MoveNext() at System.Collections.Generic.List1.AddEnumerable(IEnumerable1 enumerable) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

When I'm using System.Data.SqlClient, return query from SQL server with null value can be bypass without any error message. But when it changes to Microsoft.Data.SqlClient, return query from SQL Server must be treated with special treatment. So, How do I do/overcome this error message ? I knew this question kind of old style but how to treat non-null SqlParameterbecause of some return null column ?

The reason I'm trying to change Microsoft.Data.SqlClient namespace because near in the future some sensitive column will be encrypted using AKV (AlwaysEncrypted).

c#
sql-server
azure
asp.net-core-2.2
asked on Stack Overflow Dec 27, 2019 by Daleman • edited Dec 27, 2019 by Dharman

2 Answers

0

If I am understanding your question correctly you're saying that when the value of your sql param is the literal null, this blows up, and you do not want this to happen. You need to detect null and instead use System.DBNull.Value.

answered on Stack Overflow Jan 1, 2020 by LRFalk01
-1

You can try like change to FromSqlRaw and also change System.Data.SqlClient.SqlParameter to Microsoft.Data.SqlClient.SqlParameter

 get_Data = _applicationDbContext.Get_Data_Scholar.FromSql(
                        "EXEC [dbo].[Get_Data_Scholar] {0}", 
                         id).ToList();
answered on Stack Overflow Dec 27, 2019 by Gauravsa • edited Dec 27, 2019 by Gauravsa

User contributions licensed under CC BY-SA 3.0