Getting 'too many parameters passed' to stored procedure on ASPX page

故事扮演 提交于 2020-01-02 11:05:38

问题


I'm having trouble figuring this error out. I have a grid on an ASPX page that displays data from a stored procedure in an SQL Server 2008 database. When the page loads, I get the following error:

"Procedure or function <sp_name> has too many arguments specified."

Here is the code for the grid and the datasource:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
        DataSourceID="SqlDataSource1" ShowFooter="True" OnRowDataBound="GridView1_RowDataBound"
        AllowSorting="True">
        <Columns>
            <asp:BoundField DataField="MerchantID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
                SortExpression="MerchantID" />
            <asp:BoundField DataField="MerchantName" HeaderText="Merchant" SortExpression="MerchantName" />
            <asp:BoundField DataField="RapidTuitionID" HeaderText="RapidTuition ID" SortExpression="RapidTuitionID" />
            <asp:BoundField DataField="DateCreated" HeaderText="Enrolled" SortExpression="DateCreated" />
            <asp:TemplateField HeaderText="Commands">
                <ItemTemplate>
                    <asp:LinkButton ID="ImpersonateUserLinkButton" runat="server" OnClick="Command_Click"
                        CommandName="impersonate" CommandArgument='<%# Eval("MerchantID") %>' CssClass="table_command">Impersonate</asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <EmptyDataTemplate>
            No data to display.
        </EmptyDataTemplate>
        <PagerStyle CssClass="pager" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Development %>"
        SelectCommand="sp_GatewayMerchants" SelectCommandType="StoredProcedure">
        <SelectParameters>
            <asp:ControlParameter ControlID="PromotionPlaceHolderTop$StartDate" Name="StartDate"
                DefaultValue="1/1/2010" PropertyName="Text" Type="DateTime" />
            <asp:ControlParameter ControlID="PromotionPlaceHolderTop$EndDate" Name="EndDate"
                DefaultValue="12/31/2010" PropertyName="Text" Type="DateTime" />
            <asp:ControlParameter ControlID="PromotionPlaceHolderTop$StatusActive" DefaultValue="true"
                Name="StatusActive" PropertyName="Checked" Type="Boolean" />
            <asp:ControlParameter ControlID="PromotionPlaceHolderTop$StatusDeactive" DefaultValue="true"
                Name="StatusDeactive" PropertyName="Checked" Type="Boolean" />
        </SelectParameters>
    </asp:SqlDataSource>

Here's the code from the stored procedure:

ALTER PROCEDURE [dbo].[sp_GatewayMerchants] 
    -- Add the parameters for the stored procedure here
    @StartDate DateTime, 
    @EndDate DateTime,
    @StatusActive bit,
    @StatusDeactive bit
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT 
        m.MerchantID AS [ID],
        m.MerchantName,
        CASE m.StatusFlag WHEN 1 THEN 'Active' ELSE 'Deactive' END AS [Status],
        m.RapidTuitionID,
        m.DateCreated
    FROM
        Merchant m
    WHERE
        (CONVERT(varchar,m.DateCreated,112) BETWEEN CONVERT(varchar,CONVERT(DATETIME,@StartDate,101),112) AND CONVERT(varchar,CONVERT(DATETIME,@EndDate,101),112))
        AND
        (
            (@StatusActive = 1 AND m.StatusFlag = 1)
            OR 
            (@StatusDeactive = 1 AND m.StatusFlag = 0)
        )
    ORDER BY
        m.MerchantName
END

The datasource is passing 4 parameters, and the stored procedure is accepting 4, but when the page displays I get the error mentioned above. Am I missing something here?

EDIT: Here's the code behind for the template column. But I'm not sure how this could be causing extra parameters to the SP.

        protected void Command_Click(object sender, EventArgs e)
    {
        var merchantID = Convert.ToInt32(((LinkButton)sender).CommandArgument);

        switch (((LinkButton)sender).CommandName)
        {
            case "impersonate":
                var gs = GatewaySession.Parse(Page.User.Identity.Name);
                gs.Role = GatewaySession.RoleEnum.Merchant;
                gs.MerchantID = merchantID;
                gs.CustomerID = -1;

                FormsAuthentication.SetAuthCookie(gs.ToString(), false);

                Page.Session["MerchantID"] = gs.MerchantID;

                Response.Redirect("/Merchant/Default.aspx");
                break;
        }
    }

If I remove the ASP:LINKBUTTON the code works. So why would a LINKBUTTON be causing this?


回答1:


Use Profiler and check what parameters are really passed to SP.




回答2:


From your code it looks like you're passing 4 parameters, but let's make sure that's what's happening during design AND run time.

Design Time

In the Design view of your page, select your GridView and expand the extended menu by clicking the [>] and choose to refresh the schema. This may clear your item templates but that should be ok since you have your code saved elsewhere (i.e. here).

Run Time

Hook onto the SqlDataSource.Selecting event and check your command parameters in debug mode.

ASPX

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Development %>"
    SelectCommand="sp_GatewayMerchants" SelectCommandType="StoredProcedure"
    OnSelecting=SqlDataSource1_Selecting>
    ...

CS

protected void SqlDataSource1_Selecting(object sender, System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs e) {
    // check e.Command.Parameters
}

EDIT

Another thing you can try is set the DataKeyNames property of the Data

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
    DataSourceID="SqlDataSource1" ShowFooter="True" OnRowDataBound="GridView1_RowDataBound"
    AllowSorting="True" DataKeyNames="MerchantID">

EDIT 2

More things to try:

  1. When you're "refreshing" your page, make sure to not use F5 or hit the Refresh button. Go to your browser's address field and hit Enter as to not have IsPostBack = true
  2. Remove OnClick, CommandName, and CommandArguemnt from the LinkButton's declaration



回答3:


Try adding a SQLDataSource.Selecting event handler and check what parameters are passed to the stored procedure. The parameters can be found in SqlDataSourceSelectingEventArgs.Arguments.

The GridView might be passing unexpected parameters to the SP.




回答4:


It is security related, your web.config must indicate a different connection string setup (digest authentication or user login) while your stored procedure requires either one of them check your stored procedure security.



来源:https://stackoverflow.com/questions/4314676/getting-too-many-parameters-passed-to-stored-procedure-on-aspx-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!