获取“传递的参数过多”到 ASPX 页上的存储过程
我很难弄清楚这个错误。我在 ASPX 页面上有一个网格,用于显示 SQL Server 2008 数据库中存储过程的数据。页面加载时,我收到以下错误:
"Procedure or function <sp_name> has too many arguments specified."
这是网格和数据源的代码:
<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>
这是存储过程的代码:
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
数据源正在传递 4 个参数,存储过程正在接受 4 个参数,但是当页面显示时我收到上面提到的错误。我在这里错过了什么吗?
编辑:这是模板列背后的代码。但我不确定这如何导致 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;
}
}
如果我删除 ASP:LINKBUTTON 代码就可以工作。那么为什么 LINKBUTTON 会导致这种情况呢?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 Profiler 并检查哪些参数真正传递给 SP。
Use Profiler and check what parameters are really passed to SP.
从您的代码来看,您似乎传递了 4 个参数,但让我们确保这是设计和运行时期间发生的情况。
设计时间
在页面的设计视图中,选择 GridView 并通过单击 [>] 展开扩展菜单,然后选择刷新架构。这可能会清除您的项目模板,但这应该没问题,因为您已将代码保存在其他地方(即此处)。
在 SqlDataSource.Selecting 事件上运行时间
挂钩并在调试模式下检查命令参数。
ASPX
CS
EDIT
您可以尝试的另一件事是设置 Data
EDIT 2
的 DataKeyNames 属性更多要尝试的事情:
确保不要使用 F5 或点击
刷新按钮。转到您的浏览器
地址字段并按 Enter 键
没有 IsPostBack = true
命令参数来自
LinkButton 的声明
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
CS
EDIT
Another thing you can try is set the DataKeyNames property of the Data
EDIT 2
More things to try:
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
CommandArguemnt from the
LinkButton's declaration
尝试添加
SQLDataSource.Selecting
事件处理程序并检查向存储过程传递了哪些参数。这些参数可以在SqlDataSourceSelectingEventArgs.Arguments
中找到。GridView 可能会向 SP 传递意外参数。
Try adding a
SQLDataSource.Selecting
event handler and check what parameters are passed to the stored procedure. The parameters can be found inSqlDataSourceSelectingEventArgs.Arguments
.The GridView might be passing unexpected parameters to the SP.
它与安全相关,您的 web.config 必须指示不同的连接字符串设置(摘要式身份验证或用户登录)
而您的存储过程需要其中之一
检查您的存储过程的安全性。
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.