对于3层代码,asp.net中的sqldatasource不支持更新
本文关键字:中的 sqldatasource 不支持 更新 net asp 3层 代码 对于 | 更新日期: 2023-09-27 18:23:55
Am使用codeehind和3层存储过程更新网格视图。显示时遇到问题(除非指定UpdateCommand,否则数据源"SqlDataSource2"不支持更新。),但已对数据库进行了更新。这是我的代码
<asp:gridview runat="server" AutoGenerateColumns="False" CellPadding="4" OnRowCommand = "GVEditRate_Command"
ForeColor="#333333" GridLines="None" ID="GVEditRate" OnRowUpdating ="GVEditRate_OnRowUpdating"
DataSourceID="SqlDataSource2" >
SelectCommand="SELECT [Bill_Item], [Rate_Cat_Id], [Item_Rate], [Professional_Charge], [Anes_Charge], [Effective_Date] FROM [Rate_M] WHERE (([Active_Flag] = @Active_Flag) AND ([Bill_Item] = @Bill_Item))" >
<SelectParameters>
<asp:Parameter DefaultValue="Y" Name="Active_Flag" Type="String" />
<asp:ControlParameter ControlID="TB_ItemCode" Name="Bill_Item"
PropertyName="Text" Type="String" />
</SelectParameters>
背后的代码
protected void GVEditRate_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
Business bz = new Business();
rate_data rd = new rate_data();
rd.itemcode = (GVEditRate.Rows[e.RowIndex].FindControl("Label1") as Label).Text;
rd.ratecategory = (GVEditRate.Rows[e.RowIndex].FindControl("Label2") as Label).Text;
string itmrt = (GVEditRate.Rows[e.RowIndex].FindControl("TextBox1") as TextBox).Text;
rd.itemrate = Convert.ToDouble(itmrt);
string prf = (GVEditRate.Rows[e.RowIndex].FindControl("TextBox2") as TextBox).Text;
rd.profesionalchg = Convert.ToDouble(prf);
string anes = (GVEditRate.Rows[e.RowIndex].FindControl("TextBox3") as TextBox).Text;
rd.ansthcharge = Convert.ToDouble(anes);
rd.effective_date = DateTime.Now;
rd.LogId = Convert.ToDouble(Session["LogId"]);
rd.ShiftId = Convert.ToInt16(Session["ShiftID"]);
rd.Userid = Convert.ToString(Session["User"]);
rd.Actv_flag = "Y";
int upd_rate = bz.update_rate(rd);
if (upd_rate > 0)
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation",
"<script language='javascript'>alert('Rate updation successful')</script>");
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation",
"<script language='javascript'>alert('could not update rate')</script>");
}
}
错误消息告诉您,您需要为数据源定义一个UpdateCommand
。
您已经定义了SelectCommand,因此您的应用程序知道如何检索数据。但它不知道如何更新或插入数据,因为您没有指定它。
检查SQLDataSource并插入适当的命令:
<asp:SqlDataSource
id="SqlDataSource2"
runat="server"
SelectCommand="SELECT [Bill_Item], [Rate_Cat_Id], [Item_Rate], [Professional_Charge], [Anes_Charge], [Effective_Date] FROM [Rate_M] WHERE (([Active_Flag] = @Active_Flag) AND ([Bill_Item] = @Bill_Item))"
UpdateCommand="INSERT Stored Procedure here">
UpdateCommandType="StoredProcedure"
/* ... */>
<UpdateParameters>
<asp:Parameter Direction="Output" Name="Name1" Type="Int32" />
<asp:Parameter Direction="Output" Name="Name2" Type="Int32" />
<asp:Parameter Direction="Output" Name="Name3" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>