Passing pColumns with space as params in ExecuteSqlRaw

0

I am trying to execute a stored procedure using ExecuteSqlRaw. Normal params are getting executed properly, but when I tried to execute a stored procedure with the mandatory parameter like below

List<SqlParameter> Params = new List<SqlParameter>
    {
        new SqlParameter("ContactEMail", "test@gmail.in"),
        new SqlParameter("Name", "test" ),
        new SqlParameter("Password", "test"),
    };
SqlParameter[] parameters1 = Params.ToArray();
var res = cn.ExecuteSqlRaw("Tool_CreateCustomer  @ContactEMail, @Name, @Password", parameters1);
  

and the database column is like:

enter image description here

I am getting this error:

Microsoft.Data.SqlClient.SqlException (0x80131904): Procedure or function 'CreateCustomer' expects parameter '@Name', which was not supplied.

enter image description here

Executing in SQL works enter image description here

c#
sql-server
entity-framework
asp.net-core
asked on Stack Overflow Aug 31, 2020 by Dah Sra • edited Aug 31, 2020 by Dale K

1 Answer

2

You are only passing 3 out of 5 parameters AND you are not using the correct method to handle optional parameters. If you only wish to pass 3 parameters (and your stored procedure parameters are defined with defaults) then change your code from:

Tool_CreateCustomer @ContactEMail, @Name, @Password;

to the following where you explicitly tell SQL Server which parameters you are providing

Tool_CreateCustomer @ContactEMail = @ContactEMail, @Name = @Name, @Password = @Password;

You might think that SQL Server can deduce which parameter is which in your code, but actually when you leave out the assignment it just matches them up in ordinal order. You can name your parameters anything you like in both examples, in the first example they are mapped by order, and in the second you map them.

Official Reference

answered on Stack Overflow Aug 31, 2020 by Dale K • edited Aug 31, 2020 by Dale K

User contributions licensed under CC BY-SA 3.0