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:
I am getting this error:
Microsoft.Data.SqlClient.SqlException (0x80131904): Procedure or function 'CreateCustomer' expects parameter '@Name', which was not supplied.
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.
User contributions licensed under CC BY-SA 3.0