2013-07-25 48 views
0

Posibale重複Getting 'too many parameters passed' to stored procedure on ASPX page存儲過程的參數太多


我使用一些更新SqlDataSource控件和一個GridView中刪除的功能。我不斷收到運行時錯誤Procedure or function spUpdateTest has too many arguments specified.當我在SQL Server中運行存儲過程時,它工作正常。當我點擊GridView的刪除部分時,可以工作。但是,當我嘗試更新時,出現上述錯誤。

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
      ConnectionString="<%$ ConnectionStrings:dbcsDrugCards %>" 
      DeleteCommand="spDeleteTest" DeleteCommandType="StoredProcedure" 
      InsertCommand="spInsertTest" InsertCommandType="StoredProcedure" 
      SelectCommand="spGetAllTest" SelectCommandType="StoredProcedure" 
      UpdateCommand="spUpdateTest" UpdateCommandType="StoredProcedure"> 
      <DeleteParameters> 
       <asp:Parameter Name="PatientId" Type="Int32" /> 
      </DeleteParameters> 
      <InsertParameters> 
       <asp:Parameter Name="PatientId" Type="Int32" /> 
       <asp:Parameter Name="RaceId" Type="Int32" /> 
       <asp:Parameter Name="CountyId" Type="Int32" /> 
       <asp:Parameter Name="Gender" Type="String" /> 
       <asp:Parameter Name="DateOfBirth" Type="DateTime" /> 
       <asp:Parameter Name="SesId" Type="Int32" /> 
      </InsertParameters> 
      <UpdateParameters> 
       <asp:Parameter Name="PatientId" Type="Int32" /> 
       <asp:Parameter Name="RaceId" Type="Int32" /> 
       <asp:Parameter Name="CountyId" Type="Int32" /> 
       <asp:Parameter Name="Gender" Type="String" /> 
       <asp:Parameter Name="DateOfBirth" Type="DateTime" /> 
       <asp:Parameter Name="SesId" Type="Int32" /> 
      </UpdateParameters> 
     </asp:SqlDataSource> 

您可以在SqlDataSource中看到六個參數。現在這裏是我的存儲過程

create proc spUpdateTest 
@PatientId int 
,@RaceId int 
,@CountyId int 
,@Gender varchar(50) 
,@DateOfBirth date 
,@SesId int 
as 
begin 
update t 
set t.raceid = @RaceId 
,t.countyId = @CountyId 
,t.gender = @Gender 
,t.DateOfBirth = @DateOfBirth 
,t.SocioEconomicStatusId = @SesId 
from test as t 
where t.patientId = @PatientId 
end 

都有六個參數。 GridView控件在好的情況下測量它的確與衆不同

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
      CellPadding="4" DataKeyNames="patientId" DataSourceID="SqlDataSource1" 
      ForeColor="#333333" GridLines="None"> 
      <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
      <Columns> 
       <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" /> 
       <asp:BoundField DataField="patientId" HeaderText="patientId" ReadOnly="True" 
        SortExpression="patientId" /> 
       <asp:BoundField DataField="RaceId" HeaderText="RaceId" 
        SortExpression="RaceId" /> 
       <asp:BoundField DataField="CountyId" HeaderText="CountyId" 
        SortExpression="CountyId" /> 
       <asp:BoundField DataField="Gender" HeaderText="Gender" 
        SortExpression="Gender" /> 
       <asp:BoundField DataField="DateOfBirth" HeaderText="DateOfBirth" 
        SortExpression="DateOfBirth" /> 
       <asp:BoundField DataField="SocioEconomicStatusId" 
        HeaderText="SocioEconomicStatusId" SortExpression="SocioEconomicStatusId" /> 
       <asp:BoundField DataField="DateEnrolled" HeaderText="DateEnrolled" ReadOnly="true" 
        SortExpression="DateEnrolled" /> 
      </Columns> 
      <EditRowStyle BackColor="#999999" /> 
      <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
      <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
      <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 
      <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 
      <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> 
      <SortedAscendingCellStyle BackColor="#E9E7E2" /> 
      <SortedAscendingHeaderStyle BackColor="#506C8C" /> 
      <SortedDescendingCellStyle BackColor="#FFFDF8" /> 
      <SortedDescendingHeaderStyle BackColor="#6F8DAE" /> 
     </asp:GridView> 

我不知道這意味着什麼,但我有一個字段,DateEnrolled這並不意味着用戶可以編輯和保存到數據庫爲GetDate()每當一個Test對象被添加到數據庫。

C#

public static void UpdateTest(int PatientId, int raceID, int countyId, string gender 
      , DateTime dateOfBirth, int sesId) 
     { 
      using (SqlConnection con = new SqlConnection(TestDataAccessLayer.ConnectionString)) 
      { 
       using (SqlCommand cmd = new SqlCommand("spUpdateTest", con)) 
       { 
        con.Open(); 
        cmd.CommandType = CommandType.StoredProcedure; 
        cmd.Parameters.AddWithValue("@RaceDescription", raceID); 
        cmd.Parameters.AddWithValue("@CountyName", countyId); 
        cmd.Parameters.AddWithValue("@Gender", gender); 
        cmd.Parameters.AddWithValue("@DateOfBirth", dateOfBirth); 
        cmd.Parameters.AddWithValue("@SesDescription", sesId); 
        cmd.Parameters.AddWithValue("@PatientId", PatientId); 
        cmd.ExecuteNonQuery(); 
       } 
      } 
     } 

這個作品

public static void DeleteTest(int PatientId) 
     { 
      using (SqlConnection con = new SqlConnection(TestDataAccessLayer.ConnectionString)) 
      { 
       using (SqlCommand cmd = new SqlCommand("spDeleteTest", con)) 
       { 
        con.Open(); 
        cmd.CommandType = CommandType.StoredProcedure; 
        cmd.Parameters.AddWithValue("@PatientId", PatientId); 
        cmd.ExecuteNonQuery(); 
       } 
      } 
     } 

哪裏是這個神祕的額外的參數,從我不知道來了嗎?

+2

未來,使用Profiler來查找正在發送的確切代碼。通常一旦你看到確切的發送內容,你就可以找出問題所在。此SQl服務器特定的,但其他數據庫具有某種配置文件功能,或者您可以獲得。不要在沒有一個軟件的情況下針對數據庫編寫代碼,該軟件可以讓您分析應用程序實際發送到數據庫的內容。它將爲您節省數千個調試時間。 – HLGEM

+0

@HLGEM這是我正在尋找的建議類型:實用。 – wootscootinboogie

回答

2

您的C#代碼中的參數名稱與存儲過程中定義的名稱不匹配。

以下是不匹配的是,在存儲過程的名稱:

  • @RaceId - 這是在C#代碼@RaceDescription
  • @CountyId - 這是在C#代碼@CountryName
  • @SesId - 這是在C#代碼@SesDescription

我的猜測,沒有看到代碼,您刪除存儲過程,就是@PatientId兩個參數匹配(C#和存儲過程),所以這就是爲什麼這個工作和更新沒有。

+0

GridView控件中的所有內容都是綁定字段,因此被轉換爲存儲過程的參數。我有一個字段是'GetDate()',它沒有被使用。所以我將額外的參數添加到默認值爲null的存儲過程中,並按預期工作。 – wootscootinboogie