我无法解决这个错误。我在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会造成这种情况?
答案 0 :(得分:3)
使用Profiler并检查实际传递给SP的参数。
答案 1 :(得分:2)
从您的代码中看起来您传递了4个参数,但让我们确保在设计和运行期间发生了什么。
在页面的“设计”视图中,选择GridView并单击[&gt;]展开扩展菜单,然后选择刷新架构。这可能会清除您的项目模板,但这应该没问题,因为您已将代码保存在其他位置(即此处)。
挂钩到SqlDataSource.Selecting事件并在调试模式下检查命令参数。
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
}
修改强>
您可以尝试的另一件事是设置Data
的DataKeyNames属性<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" ShowFooter="True" OnRowDataBound="GridView1_RowDataBound"
AllowSorting="True" DataKeyNames="MerchantID">
编辑2
更多尝试:
答案 2 :(得分:0)
尝试添加SQLDataSource.Selecting
事件处理程序,并检查传递给存储过程的参数。参数可以在SqlDataSourceSelectingEventArgs.Arguments
中找到。
GridView可能会将意外参数传递给SP。
答案 3 :(得分:0)
与安全性相关,您的web.config必须指明不同的连接字符串设置(摘要式身份验证或用户登录) 而您的存储过程需要其中一个 检查存储过程的安全性。