简体   繁体   English

过程指定了太多参数

[英]Procedure has too many arguments specified

I'm experiencing some problems in updates. 我在更新中遇到了一些问题。 I had to build my customized select and so I couldn't make dynamic queries. 我必须构建自己的自定义选择,因此无法进行动态查询。 I've been trying to update my database using the code shown below, but I always get this error: 我一直在尝试使用下面显示的代码更新数据库,但是我总是会收到此错误:

Procedure or function UpdateUser has too many arguments specified. 过程或函数UpdateUser指定了太多参数。

I've filled all the arguments, no more, no less.... what's wrong? 我已经填满了所有论点,没有更多,没有更少....怎么了?

ASPX markup: ASPX标记:

<asp:SqlDataSource ID="AllUsers" runat="server" 
    ConnectionString="<%$ ConnectionStrings:LocalSQL %>"
    SelectCommand="TodosOsUtilizadores" SelectCommandType="StoredProcedure">
</asp:SqlDataSource>

C# code-behind: C#代码背后:

using (SqlDataSource ds = AllUsers)
{
    Guid guid = new Guid();

    try
    {
        guid = Guid.Parse(Session["user_id"].ToString());
    }
    catch 
    { 
        guid = Guid.NewGuid(); 
    }

    ds.UpdateCommand = "UpdateUser";
    ds.ConflictDetection = ConflictOptions.OverwriteChanges;
    ds.UpdateCommandType = SqlDataSourceCommandType.StoredProcedure;
    ds.UpdateParameters.Add("userid", guid.ToString());
    ds.UpdateParameters.Add("username", "Diogo");
    ds.UpdateParameters.Add("email", "user_test@gmail.com");
    ds.UpdateParameters.Add("isAnonimo", "0");
    ds.UpdateParameters.Add("isLocked", "0");
    ds.UpdateParameters.Add("roleId", "");

    ds.Update();
    DetailsView1.DataBind();
}

Stored procedure: 存储过程:

CREATE PROCEDURE [dbo].UpdateUser
    @userid nvarchar(max),
    @username nvarchar(200),
    @email nvarchar(500),
    @isAnonimo bit, 
    @isLocked bit,
    @roleid nvarchar(max)
AS
    UPDATE Memberships 
    SET Email = @email, 
        IsLockedOut = @isLocked 
    WHERE 
        UserId = CAST(@userid as uniqueidentifier);

    UPDATE Users 
    SET UserName = @username, 
        IsAnonymous = @isAnonimo 
    WHERE 
        UserId = CAST(@userid AS uniqueidentifier);

    IF (@roleid IS NULL) 
    BEGIN
        UPDATE UsersInRoles 
        SET RoleId = CAST(roleid AS uniqueidentifier);
    END;

The issue is with how the SqlDataSource works, specifically the ConflictDetection . 问题在于SqlDataSource工作方式,特别是ConflictDetection

As MSDN says : 正如MSDN所说

The ConflictDetection property determines whether parameters for old and new values are applied to the Update method. ConflictDetection属性确定旧值和新值的参数是否应用于Update方法。 For example, if the command that is specified by the SelectCommand property returns a DataTable object with the columns Name and Number and the ConflictDetection property is set to the OverwriteChanges value, parameters are created for Name and Number for the Update method. 例如,如果由SelectCommand属性指定的命令返回带有Name和Number列的DataTable对象,并且ConflictDetection属性设置为OverwriteChanges值, 则会为Update方法的Name和Number创建参数。

So if your SelectCommand returns eg firstname , then there is also a firstname parameter already in your UpdateCommand and that is what is causing the errors. 因此,如果您的SelectCommand返回例如firstname ,则UpdateCommand已经存在一个firstname参数,这就是导致错误的原因。

Not sure if it will work, but a simple thing to try would be clearing the UpdateParameters collection first: 不知道它是否会起作用,但是一个简单的尝试是首先清除UpdateParameters集合:

using (SqlDataSource ds = AllUsers)
{
    Guid guid = new Guid();
    try
    {
        guid = Guid.Parse(Session["user_id"].ToString());
    }
    catch { guid = Guid.NewGuid(); }

    ds.UpdateCommand = "UpdateUser";
    ds.ConflictDetection = ConflictOptions.OverwriteChanges;
    ds.UpdateCommandType = SqlDataSourceCommandType.StoredProcedure;

    ds.UpdateParameters.Clear();

    ds.UpdateParameters.Add("userid", guid.ToString());
    ds.UpdateParameters.Add("username", "Diogo");
    ds.UpdateParameters.Add("email", "user_test@gmail.com");
    ds.UpdateParameters.Add("isAnonimo", "0");
    ds.UpdateParameters.Add("isLocked", "0");
    ds.UpdateParameters.Add("roleId", "");

    ds.Update();
    DetailsView1.DataBind();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM