Home > Software engineering >  Database.ExecuteSqlRaw - only accepts non-null SqlParameter type objects
Database.ExecuteSqlRaw - only accepts non-null SqlParameter type objects

Time:12-22

I try to call a method containing code like the one below

The code is to be execute by the procedure

Unfortunately, I ran into an error, I described the error below

does anyone know of any other solution to execute the procedure?

Controller:


string fromMail = "test";
string toMail= "test";
string title = "test";
string body= "test";


   string sql = @"
                        
                        USE [eFaktura]
                        GO
                        
                        DECLARE @ExtSystem varchar(20)
                        DECLARE @AdresOd varchar(255)
                        DECLARE @AdresDo varchar(255)
                        DECLARE @Temat varchar(255)
                        DECLARE @Tresc varchar(max)
                        DECLARE @Logo bit
                        DECLARE @IDMaila int
                        
                        
                        EXECUTE [dbo].[WstawMail_X] 
                           @ExtSystem = @ParExtSystem
                          ,@AdresOd = @ParAdresOd
                          ,@AdresDo = @ParAdresDo
                          ,@Temat = @ParTemat
                          ,@Tresc = @ParTresc
                          ,@Logo = 0
                          ,@IDMaila = @IDMaila output
                        GO
                        
                        ";
            _context.Database.ExecuteSqlRaw(sql,

                 new SqlParameter("@ParExtSystem", "ECP"),
                 new SqlParameter("@ParAdresOd", fromMail),
                 new SqlParameter("@ParAdresDo", toMail),
                 new SqlParameter("@ParTemat", title),
                 new SqlParameter("@ParTresc", body),
                 new SqlParameter("@ParLogo", 0)

            );

System.InvalidCastException HResult=0x80004002 Message=The SqlParameterCollection only accepts non-null SqlParameter type objects, not > SqlParameter objects. Source=Microsoft.Data.SqlClient StackTrace: at Microsoft.Data.SqlClient.SqlParameterCollection.ValidateType(Object value) at Microsoft.Data.SqlClient.SqlParameterCollection.Add(Object value) at > Microsoft.EntityFrameworkCore.Storage.Internal.RawRelationalParameter.AddDbParameter(DbComma > nd command, Object value)

CodePudding user response:

It might be easier to just call the sproc directly:

SqlParameter idm;

_context.Database.ExecuteSqlRaw(
     "[dbo].[WstawMail_X] @pEx, @pAOd, @pADo, @pTe, @pTr, @pL, @pIDM OUTPUT",
     new SqlParameter("@pEx", "ECP"),
     new SqlParameter("@pAOd", fromMail),
     new SqlParameter("@pADo", toMail),
     new SqlParameter("@pTe", title),
     new SqlParameter("@pTR", body),
     new SqlParameter("@pL", 0),
     idm = new SqlParameter("@pIDM", 0)
);
//you can retrieve the value of pIDM here if you want

Or if you want to make life neater, use interpolated:

SqlParameter idm = new SqlParameter("@whateverName", 0)
_context.Database.ExecuteSqlInterpolated(
     $"[dbo].[WstawMail_X] 'ECP', {fromMail}, {toMail}, {title}, {body}, '0', {idm} OUTPUT"
);
//retrieve idm.Value here

If you're using named because that above isn't the order, or you just like to use named, it's a bit clearer with interpolated:

_context.Database.ExecuteSqlInterpolated(
     $"[dbo].[WstawMail_X] @ExtSystem='ECP', @ParAdresOd={fromMail}, ..., @IDMaila={idm} OUTPUT"
);
  • Related