【问题标题】:SWL Error Function has too many arguments specifiedSWL 错误函数指定了太多参数
【发布时间】:2011-05-05 07:24:39
【问题描述】:

我正在使用 ASP.NET 3.5SQL Server 2008

我有一个SQLDataSourceGridview。我正在尝试使用 SQL 中的 存储过程 同时更新 2 个表。 SQLDatasource 正在传递 7 个参数。 存储过程需要的5个参数,返回值&StudentID。

不确定错误是在我的SQLDatasorce 还是我的存储过程

这是我的 ASPX 代码:

 <asp:SqlDataSource ID="sqldsUserLoginNLevels" runat="server" 
     ConnectionString="<%$ ConnectionStrings:QuizStarConnectionString %>"

 SelectCommand="SELECT UserLogins.StudentID, UserLogins.StudentName, UserLogins.UserID,
      UserLogins.Password, UserLevels.GrammarStart, UserLevels.GrammarCurrent,
      UserLevels.MathStart, UserLevels.MathCurrent 
      FROM UserLogins 
      INNER JOIN UserLevels ON UserLogins.StudentID = UserLevels.StudentID" 

  DeleteCommand="DELETE FROM [UserLogins] WHERE [StudentID] = @original_StudentID"

  InsertCommand="INSERT INTO [UserLogins] ([StudentName], [UserID], [Password]) 
     VALUES (@StudentName, @UserID, @Password)" 

  UpdateCommand="UpdateUserLoginsAndUserLevels" 

UpdateCommandType="StoredProcedure" >

    <DeleteParameters>
        <asp:Parameter Name="original_StudentID" />
    </DeleteParameters>

    <UpdateParameters>
        <asp:Parameter Name="StudentName" Type="String" />
        <asp:Parameter Name="UserID" Type="String" />
        <asp:Parameter Name="Password" Type="String" />
        <asp:Parameter Name="GrammarStart" Type="String" />
        <asp:Parameter Name="MathStart" Type="String" />
        <asp:Parameter Direction="ReturnValue" Name="RETURN_VALUE" Type="Int32" />
    </UpdateParameters>

    <InsertParameters>
        <asp:Parameter Name="StudentName" Type="String" />
        <asp:Parameter Name="UserID" Type="String" />
        <asp:Parameter Name="Password" Type="String" />
    </InsertParameters>

</asp:SqlDataSource>

我的存储过程是:

ALTER PROCEDURE [dbo].[UpdateUserLoginsAndUserLevels] (  
@StudentName    VARCHAR(50), 
@UserID     NCHAR(10), 
@Password   NCHAR(10), 
@GrammarStart   NCHAR(10), 
@MathStart  NCHAR(10))

AS 
DECLARE @StudentID INT; 


BEGIN Transaction 
    BEGIN TRY         

        SELECT * From UserLogins                   
            Where StudentID = @StudentID 

        UPDATE UserLogins                  
            SET 
                StudentName= @StudentName,
                UserID = @UserID,
                Password = @Password
            Where StudentID = @StudentID  

        UPDATE UserLevels                  
            SET 
                GrammarStart= @GrammarStart,
                MathStart = @MathStart
                FROM UserLevels
                INNER JOIN UserLogins ON UserLogins.StudentID = UserLevels.StudentID 
            WHERE (UserLevels.StudentID =  @StudentID)
    END TRY 

    BEGIN CATCH         
        DECLARE @ErrorMessage NVARCHAR(4000), @ErrorSeverity INT         
        -- Assign variables to error-handling functions that          
        -- capture information for RAISERROR.         
        SELECT @ErrorMessage = ERROR_MESSAGE(), @ErrorSeverity = ERROR_SEVERITY()         
        -- Rollback the failed transaction         
        ROLLBACK;         
        -- Raise an error: with the original error information.         
        RAISERROR(@ErrorMessage, @ErrorSeverity, 1); 
    END CATCH 
COMMIT Transaction; 

还没有想好如何发布代码。对不起。

【问题讨论】:

  • 发布代码缩进 4 个空格。 (选择它并点击{} 图标来执行此操作)
  • 是否有任何东西绑定到这个数据源?你在后面的代码中完全弄乱了参数吗?

标签: asp.net sql-server-2008


【解决方案1】:

确保您没有重复添加参数。由于您将它们添加到 aspx 页面中,因此您不需要将它们添加到后面的代码中。

例如,如果您的代码中包含以下内容:

sqldsUserLoginNLevelsUserID.UpdateParameters.Add(new Parameter("UserID ", TypeCode.Int32)); 

它可以解释你的错误。

如果您需要从后面的代码中设置值,请这样做:

sqldsUserLoginNLevelsUserID.UpdateParameters["UserID "].DefaultValue = "1";

【讨论】:

  • 我将存储过程更改为: ALTER PROCEDURE [dbo].[UpdateUserLoginsAndUserLevels] (@StudentID INT, @StudentName VARCHAR(50), @UserID NCHAR(10), @Password NCHAR( 10), @GrammarStart NCHAR(10), @MathStart NCHAR(10)) AS --DECLARE @StudentID INT;它现在可以工作了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-28
相关资源
最近更新 更多