2011-11-11 75 views
2

我試圖獲取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 
+0

您可以包括存儲過程的副本? –

+0

我標記了存儲過程,因此更容易找到。抱歉。 – jlg

+0

對不起,不知道我錯過了什麼,但我們需要看看聲明和接受的參數。即創建過程getCategoryAncestorsByProductID(@id int)AS –

回答

0

您需要刪除類別ID參數:

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

,還應注意:您的存儲過程只有r eturns CategoryName,而不是CategoryID。您應該能夠通過更改下面一行在存儲過程來解決這個問題:

SELECT CategoryName FROM #FamilyTree ORDER BY OrderNumber DESC 

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

那麼,頁面上再也沒有錯誤,所以石頭!謝謝! – jlg

相關問題