wootscootinboogie
wootscootinboogie

Reputation: 8695

Stored Procedure has too many parameters


Posibale Duplicate Getting 'too many parameters passed' to stored procedure on ASPX page


I'm using a SqlDataSource control for some update and delete functionality within a GridView. I keep getting the runtime error Procedure or function spUpdateTest has too many arguments specified. When I run the stored procedure in SQL Server it works fine. When I click on the delete portion of the GridView, that works. However, when I try to update I get the above error.

 <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>

You can see the six parameters in the SqlDataSource. Now here is my stored procedure

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  

both have six parameters. GridView for good measure in case it makes a difference

   <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>

I don't know if it means anything, but I do have one field, DateEnrolled which is not meant to be edited by the user and is saved to the database as GetDate() whenever a Test object is added to the database.

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();
                }
            }
        }

this works

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();
                }
            }
        }

Where is this mysterious extra parameter coming from that I don't know about?

Upvotes: 0

Views: 2793

Answers (1)

Karl Anderson
Karl Anderson

Reputation: 34844

Your parameter names in your C# code do not match what is defined in the stored procedure.

Here are the names that do not match what is in the stored procedure:

  • @RaceId - it is @RaceDescription in the C# code
  • @CountyId - it is @CountryName in the C# code
  • @SesId - it is @SesDescription in the C# code

My guess, without seeing the code, for your delete stored procedure, is that the @PatientId parameters matches on both sides (C# and stored procedure) so that is why that one works and the update does not.

Upvotes: 2

Related Questions