过程或函数指定了太多参数

时间:2011-11-11 20:59:30

标签: vb.net sql-server-2008 listview stored-procedures

我正在尝试获取CategoryID的值,以便在管理员想要更新产品功能时我可以将该值插入数据库。编写的存储过程是针对ListView的,所以我希望我可以在其中隐藏一些东西来获取ID。当我在页面上放置隐藏字段并尝试获取ID时,我收到了标题中提到的错误。

 <ul id="categories">
        <asp:ListView ID="lvAncestorCategories" runat="server" DataSourceID="dsCategoryAncestors">
            <ItemTemplate>
            <li><%# Eval("CategoryName") %></li>
            <li>
                <asp:HiddenField ID="CategoryID" runat="server" Value='<%# Eval("CategoryID") %>' />
            </li>
            </ItemTemplate>
            <ItemSeparatorTemplate> > </ItemSeparatorTemplate>
        </asp:ListView>
    </ul>


<asp:SqlDataSource ID="dsCategoryAncestors" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ProductsConnectionString %>" 
    ProviderName="<%$ ConnectionStrings:ProductsConnectionString.ProviderName %>" 
    SelectCommand="getCategoryAncestorsByProductID" 
    SelectCommandType="StoredProcedure">
    <SelectParameters>
        <asp:QueryStringParameter Name="ProductID" QueryStringField="id" 
            Type="Int32" />
    </SelectParameters>
    <SelectParameters>
        <asp:QueryStringParameter Name="CategoryID" QueryStringField="id" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>

我没有编写存储过程,所以我不知道它在做什么,我得到一个错误,说我的listview没有@CategoryID的值,所以我将它添加到SELECT语句中的存储过程。不知道存储过程是如何工作的,我得到了“太多的参数”错误。

Dim featureSql As String = "INSERT INTO Marketing(ProductID, 
MarketingTypeID, MarketingTitle, MarketingData) 
VALUES ( @ProductID, 3, 'Feature', @MarketingData);
UPDATE Product SET ModifyDate = getdate(), 
ModifyUser = @ModifyUser 
WHERE ProductID = @ProductID; 
INSERT INTO Feature (FeatureTitle, CategoryID) 
VALUES (@FeatureTitle, @CategoryID)"

            Using cn As New SqlConnection
            (System.Configuration.ConfigurationManager.ConnectionStrings
            ("LocalSqlServer").ConnectionString)

                Using cmd As New SqlCommand(featureSql, cn)

                    cmd.Parameters.Add(New SqlParameter("@FeatureTitle", txtFeature.Text))
                    cmd.Parameters.Add(New SqlParameter("@CategoryID", CategoryID.Value))
                End Using
            End Using
<asp:SqlDataSource ID="dsCategoryAncestors" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ProductsConnectionString %>" 
    ProviderName="<%$ ConnectionStrings:ProductsConnectionString.ProviderName %>" 
    SelectCommand="getCategoryAncestorsByProductID" 
    SelectCommandType="StoredProcedure">
    <SelectParameters>
        <asp:QueryStringParameter Name="ProductID" QueryStringField="id" 
            Type="Int32" />
    </SelectParameters>
    <SelectParameters>
        <asp:QueryStringParameter Name="CategoryID" QueryStringField="id" Type="Int32"  />
    </SelectParameters>
</asp:SqlDataSource>

存储过程:

USE [Products]
GO
/****** Object:  StoredProcedure [dbo].[getCategoryAncestorsByProductID]    
Script Date: 11/11/2011 13:28:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER PROCEDURE [dbo].[getCategoryAncestorsByProductID]
-- Add the parameters for the stored procedure here
@ProductID int
AS
BEGIN
-- This stored procedure gets all the lineage of a particular category

-- Create a temp table to hold the lineage in order
CREATE TABLE #FamilyTree (
    OrderNumber int,
    CategoryID int,
    CategoryName nvarchar(max)
)

DECLARE @CategoryID nvarchar(max)
SET @CategoryID = 
   (SELECT CategoryID 
    FROM CategoryLink 
    WHERE ProductID = @ProductID)

DECLARE @CategoryName nvarchar(max)
SELECT @CategoryName = CategoryName FROM Category WHERE CategoryID = @CategoryID

-- Insert child category into table first giving it an OrderNumber of 1 
    -- the higher the number, the higher in the tree
INSERT INTO #FamilyTree VALUES (1,@CategoryID,@CategoryName)

-- Use this for the OrderNumber. Increment by 1 for each level.
DECLARE @OrderNumber int
SET     @OrderNumber = 2

-- Use this to hold the new CategoryID to add to the familytree table
DECLARE @AncestorID INT

WHILE (SELECT COUNT(*) FROM Category 
           WHERE CategoryID = 
          (SELECT ParentID FROM Category 
           WHERE CategoryID = @CategoryID)) > 0
BEGIN

    SELECT @AncestorID = CategoryID, @CategoryName = CategoryName 
            FROM Category WHERE CategoryID = 
            (SELECT ParentID FROM Category 
            WHERE CategoryID = @CategoryID)

    INSERT INTO #FamilyTree VALUES (@OrderNumber,@AncestorID,@CategoryName)
    SET @OrderNumber = @OrderNumber + 1
    SET @CategoryID = @AncestorID --Moves to the next CategoryID to see if it
                                            has any parents

END

--Returns Categories in order from Parents to Children
SELECT CategoryName FROM #FamilyTree ORDER BY OrderNumber DESC

DROP TABLE #FamilyTree

END

这就是我想要的工作:

Dim featureSql As String = "INSERT INTO Marketing
                            (ProductID, MarketingTypeID, MarketingTitle, MarketingData) 
                            VALUES ( @ProductID, 3, 'Feature', @MarketingData);
                            UPDATE Product 
                            SET ModifyDate = getdate(), ModifyUser = @ModifyUser 
                            WHERE ProductID = @ProductID; 
                            INSERT INTO Feature (FeatureTitle, CategoryID) 
                            VALUES (@FeatureTitle, @CategoryID)"
            Using cn As New SqlConnection
            (System.Configuration.ConfigurationManager.ConnectionStrings
            ("LocalSqlServer").ConnectionString)

                Using cmd As New SqlCommand(featureSql, cn)

                    cmd.Parameters.Add(New SqlParameter("@FeatureTitle", txtFeature.Text))
                    cmd.Parameters.Add(New SqlParameter("@CategoryID", CategoryID.Value))
                End Using
            End Using

1 个答案:

答案 0 :(得分:0)

您需要删除CategoryID参数:

<SelectParameters>
    <asp:QueryStringParameter Name="CategoryID" QueryStringField="id" Type="Int32" />
</SelectParameters>

另外一个注意事项:您的存储过程仅返回CategoryName,而不返回CategoryID。您应该能够通过更改存储过程中的以下行来解决此问题:

SELECT CategoryName FROM #FamilyTree ORDER BY OrderNumber DESC

SELECT CategoryName, AncestorID As CategoryID FROM #FamilyTree ORDER BY OrderNumber DESC