Search code examples
c#sql-serverdevexpresssqldatasourcetreelist

SQLDataSource = "Procedure or function [Name] has too many arguments specified."


I am not sure if its asp:SqlDataSource control or it's the ASPxTreeList from DevExpress I am using but I am getting a weird error when trying to fire a Stored Procedure using the Insert() method.

When adding a node I try to insert the node into the database

   protected void TagList_NodeInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
    {
        SqlDataTagging.InsertCommandType = SqlDataSourceCommandType.StoredProcedure;
        SqlDataTagging.InsertParameters.Add("ParentID", e.NewValues["ParentTag_ID"].ToString());
        SqlDataTagging.InsertParameters.Add("TagName", e.NewValues["TagName_VC"].ToString());
        SqlDataTagging.InsertParameters.Add("UserID", "1");

        SqlDataTagging.InsertCommand = "sp_InsertTag";

        SqlDataTagging.Insert();
    }

However when I fire the above code, I get this error

Procedure or function sp_InsertTag has too many arguments specified.

My SP header

ALTER PROCEDURE [dbo].[sp_InsertTag] (@ParentID INT, @TagName VARCHAR(100), @UserID int)

But as you can see, there are 3 parameters in the SP and 3 in the C# code.

Weirdly, if I actually run the SQL via text e.g.

SqlDataTagging.InsertCommandType = SqlDataSourceCommandType.Text; 

and write the SQL in C#, it will work...

Anyone have any ideas or any alternatives so I can use Stored Procedures?

Thanks


Solution

  • Assume you have SqlDataSource with ID SqlDataTagging to populate tree nodes, the error occurs if TagList_NodeInserting method fires more than once, hence at certain point InsertParameters collection populates with more than 3 parameters specified in stored procedure.

    To mitigate this behavior, I suggest you clearing InsertParameters collection before adding parameters like below:

    ...
    SqlDataTagging.InsertCommandType = SqlDataSourceCommandType.StoredProcedure;
    SqlDataTagging.InsertParameters.Clear(); // add this line to clear InsertParameters collection before adding parameters
    SqlDataTagging.InsertParameters.Add("ParentID", e.NewValues["ParentTag_ID"].ToString());
    SqlDataTagging.InsertParameters.Add("TagName", e.NewValues["TagName_VC"].ToString());
    SqlDataTagging.InsertParameters.Add("UserID", "1");
    ...
    

    Related issue:

    ASPxTreeList - The 'Procedure or function [name] has too many arguments specified' exception is raised when a node is deleted (similar approach)