Posibale Duplicate在 ASPX 页面上将“太多参数传递”到存储过程
我正在使用 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();
}
}
}
这个我不知道的神秘额外参数来自哪里?