我在我的web应用程序中使用ObjectDataSource而不是SqlDataSource创建了一个数据访问层。我有一个FormView来更新我的数据库中的一些数据。在我的旧asp.net代码中,我有类似的东西:从SqlDataSource转换为ObjectDataSource,导致悲伤
<asp:SqlDataSource ID="sdsTradeDetails" runat="server"
ConnectionString="<%$ ConnectionStrings:ForexDB %>"
SelectCommand="usp_GetTrade" SelectCommandType="StoredProcedure"
UpdateCommand="usp_UpdateTrade" UpdateCommandType="StoredProcedure"
<SelectParameters>
<asp:ControlParameter Name="tradeId" ControlID="grdTrades" PropertyName="SelectedDataKey.Value" />
</SelectParameters>
<UpdateParameters>
<asp:ControlParameter Name="tradeId" ControlId="frmTrade" PropertyName="SelectedValue" />
</UpdateParameters>
</asp:SqlDataSource>
哪些工作正常。我已经取代了SqlDataSource的这个:
<asp:ObjectDataSource
id="srcTrade"
TypeName="DatabaseComponent.DBUtil"
SelectMethod="GetTrade"
UpdateMethod="UpdateTrade"
runat="server">
<SelectParameters>
<asp:QueryStringParameter Name="tradeId" QueryStringField="tradeId" />
</SelectParameters>
<UpdateParameters>
<asp:ControlParameter Name="tradeId" ControlId="frmTrade" PropertyName="SelectedValue" />
</UpdateParameters>
</asp:ObjectDataSource>
但现在当我在FormView控件点击更新按钮,我得到这个错误:
异常详细信息: System.InvalidOperationException: ObjectDataSource控件 'srcTrade'不能 找到一个非泛型方法 'UpdateTrade' 有参数: 符号,pctAccountRisked, tradeSetupId,lotsPerUnit, initialStopPrice,tfCode,MAEPips, MFEPips,tradeGrade,executionGrade, tradeTypeId,comment,tradeId。
在我DBUtil类我有这样的UpdateTrade:
public void UpdateTrade(
int tradeId,
string symbol,
decimal pctAccountRisked,
string tradeSetupId,
decimal lotsPerUnit,
decimal initialStopPrice,
string tfCode,
int MAEPips,
int MFEPips,
int tradeGrade,
int executionGrade,
string comment)
{
SqlCommand cmd = new SqlCommand("usp_UpdateTrade");
cmd.Parameters.AddWithValue("@tradeId", tradeId);
cmd.Parameters.AddWithValue("@symbol", symbol);
cmd.Parameters.AddWithValue("@pctAccountRisked", pctAccountRisked);
cmd.Parameters.AddWithValue("@tradeSetupId", tradeSetupId);
cmd.Parameters.AddWithValue("@lotsPerUnit", lotsPerUnit);
cmd.Parameters.AddWithValue("@initialStopPrice", initialStopPrice);
cmd.Parameters.AddWithValue("@tfCode", tfCode);
cmd.Parameters.AddWithValue("@MAEPips", MAEPips);
cmd.Parameters.AddWithValue("@MFEPips", MFEPips);
cmd.Parameters.AddWithValue("@tradeGrade", tradeGrade);
cmd.Parameters.AddWithValue("@executionGrade", executionGrade);
cmd.Parameters.AddWithValue("@comment", comment);
UpdateTable(cmd, "trade");
}
这对于GetTrade:
public DataTable GetTrade(int tradeId)
{
SqlCommand cmd = new SqlCommand("usp_GetTrade");
cmd.Parameters.AddWithValue("@tradeId", tradeId);
return FillDataTable(cmd, "trade");
}
请帮帮忙!
感谢您的回答。我刚刚检查了参数。我不确定你的意思,但是我所做的是检查GetTrade返回的列是否存在于UpdateTrade中。 GetTrade返回的一些额外的列在UpdateTrade中不存在,但这正是我想要的。我还能检查什么?我是否需要列出UpdateTrade中的每个更新参数?当使用SqlDataSource时,我不需要这样做,我只是将主键(tradeId)添加为UpdateTrade的唯一参数。 – 2010-11-09 09:49:09
是的,您需要将UpdateTrade方法的所有参数设置为UpdateParameters(或者必须在更新触发前将其添加到代码隐藏中)。 – patmortech 2010-11-09 11:22:12
@patmortech - 非常感谢。现在都在工作。 – 2010-11-09 14:43:49