0

I attempted to passing value from object params array to each parameter of stored procedure using loop, but I get this error

'Procedure or function Proc_GetPaging_Product has too many arguments specified.'.

The problem is cmd.Parameters already has parameters when using SqlCommandBuilder.DeriveParameters(cmd) so no need to add value but assigning.

    public ServiceResult<T> GetPaging<T>(params object[] values)
    {
        var result = new ServiceResult<T>();

        using(_conn)
        {
            _conn.Open();                

            using (SqlCommand cmd = _conn.CreateCommand())
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "[SalesLT].[Proc_GetPaging_Product]";
                SqlCommandBuilder.DeriveParameters(cmd);

                int lengthParams = cmd.Parameters.Count;

                for (int i = 1; i < lengthParams; i++)
                {
                    cmd.Parameters.AddWithValue(cmd.Parameters[i].ParameterName, values[i - 1]);
                }                                             

                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var item = Activator.CreateInstance<T>();
                        var properties = typeof(T).GetProperties();

                        foreach (var property in properties)
                        {                               
                            //Type convertTo = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
                            property.SetValue(item, reader[property.Name], null);
                        }

                        result.ListResult.Add(item);
                    }

                    reader.NextResult();
                }
            }
        }

        return result;
    }

Error I got in debugger

2
  • Maybe your overloading it? Commented Oct 27, 2019 at 5:32
  • SqlCommandBuilder.DeriveParameters(cmd) adds parameters to command based on the definition of stored proc. And cmd.Parameters.AddWithValue adds new parameter. So basically you are trying to add the same parameters again to the command. Commented Oct 27, 2019 at 6:01

1 Answer 1

4

You're adding the parameters twice.

Once in this row:

SqlCommandBuilder.DeriveParameters(cmd);

and once for each parameter in the loop:

for (int i = 1; i < lengthParams; i++)
{
    cmd.Parameters.AddWithValue(cmd.Parameters[i].ParameterName, values[i - 1]);
}

Instead of using AddWithValue, simply do cmd.Parameter[i].Value = values[i].

Also, the parameters collection, like any other built-in collection in .Net, is zero based and not one based.

You loop should look like this:

for (int i = 0; i < lengthParams; i++)
{
    cmd.Parameters[i].Value = values[i];
}

Also, I you should add a condition before the loop to make sure the number of values you have match the number of parameters the stored procedure have.

And one more note: you should consider setting the parameters in the code instead of using DeriveParameters: Official documentation clearly states:

DeriveParameters requires an additional call to the database to obtain the information. If the parameter information is known in advance, it is more efficient to populate the parameters collection by setting the information explicitly.

Sign up to request clarification or add additional context in comments.

2 Comments

I modified my code follow your answer then it work, Thanks for your suggestion.
Glad to help :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.