问题描述
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;publicpartialclassUserControl_Books:System.Web.UI.UserControl{protectedvoidPage_Load(objectsender,EventArgse){}//编辑protectedvoidDataList1_EditCommand(objectsource,DataListCommandEventArgse){DataList1.EditItemIndex=e.Item.ItemIndex;DataList1.DataBind();}//取消protectedvoidDataList1_CancelCommand(objectsource,DataListCommandEventArgse){DataList1.EditItemIndex=-1;DataList1.DataBind();}//更新protectedvoidDataList1_UpdateCommand(objectsource,DataListCommandEventArgse){Int32ClassId;if(((DropDownList)e.Item.FindControl("ClassName")).SelectedValue=="0"){ClassId=int.Parse(((TextBox)e.Item.FindControl("ClassId")).ToString());}else{ClassId=int.Parse(((DropDownList)e.Item.FindControl("ClassName")).SelectedValue);}StringBookName=((TextBox)e.Item.FindControl("BookName")).ToString();StringAuthor=((TextBox)e.Item.FindControl("Author")).ToString();StringPublish=((TextBox)e.Item.FindControl("Publish")).ToString();DecimalPrice=Decimal.((TextBox)e.Item.FindControl("Price")).ToString();StringImage=((TextBox)e.Item.FindControl("Image")).ToString();Int32Qty=int.Parse(((TextBox)e.Item.FindControl("Qty")).ToString());Int32PageNum=int.Parse(((TextBox)e.Item.FindControl("PageNum")).ToString());StringBinDing=((TextBox)e.Item.FindControl("BinDing")).ToString();Int32Format=int.Parse(((TextBox)e.Item.FindControl("Format")).ToString());StringISBN=((TextBox)e.Item.FindControl("ISBN")).ToString();StringDescn=((TextBox)e.Item.FindControl("Descn")).ToString();StringContent=((TextBox)e.Item.FindControl("Content")).ToString();StringBookId=DataList1.DataKeys[e.Item.ItemIndex].ToString();SqlDataSource2.UpdateParameters["ClassId"].DefaultValue=ClassId;SqlDataSource2.UpdateParameters["BookName"].DefaultValue=BookName;SqlDataSource2.UpdateParameters["Author"].DefaultValue=Author;SqlDataSource2.UpdateParameters["Publish"].DefaultValue=Publish;SqlDataSource2.UpdateParameters["Price"].DefaultValue=Price;SqlDataSource2.UpdateParameters["Image"].DefaultValue=Image;SqlDataSource2.UpdateParameters["Qty"].DefaultValue=Qty;SqlDataSource2.UpdateParameters["PageNum"].DefaultValue=PageNum;SqlDataSource2.UpdateParameters["BinDing"].DefaultValue=BinDing;SqlDataSource2.UpdateParameters["Format"].DefaultValue=Format;SqlDataSource2.UpdateParameters["ISBN"].DefaultValue=ISBN;SqlDataSource2.UpdateParameters["Descn"].DefaultValue=Descn;SqlDataSource2.UpdateParameters["Content"].DefaultValue=Content;SqlDataSource2.UpdateParameters["BookId"].DefaultValue=BookId;SqlDataSource2.Update();DataList1.EditItemIndex=-1;DataList1.DataBind();}}
解决方案
解决方案二:
这么多看得就头疼你还是用linq吧
解决方案三:
改了一下,把UpdateCommand里面的类型全部改成String,在SqlDataSource的更新参数写对应的数据类型,可是更新的时候显示“输入字符串的格式不正确”<asp:SqlDataSourceID="SqlDataSource2"runat="server"ConnectionString="<%$ConnectionStrings:BookShopConnectionString%>"SelectCommand="SELECT*FROM[Book-Class]WHERE([BookId]=@BookId)"UpdateCommand="UPDATE[Book]SET[BookName]=@BookName,[ClassId]=@ClassId,[Author]=@Author,[Publish]=@Publish,[Price]=@Price,[Image]=@Image,[Qty]=@Qty,[PageNum]=@PageNum,[BinDing]=@BinDing,[Format]=@Format,[ISBN]=@ISBN,[Descn]=@Descn,[Content]=@ContentWHERE[BookId]=@BookId"><SelectParameters><asp:ControlParameterControlID="GridView1"Name="BookId"PropertyName="SelectedValue"Type="Int32"/></SelectParameters><UpdateParameters><asp:ParameterName="BookName"Type="String"/><asp:ParameterName="ClassId"Type="Int32"/><asp:ParameterName="Author"Type="String"/><asp:ParameterName="Publish"Type="String"/><asp:ParameterName="Price"Type="Decimal"/><asp:ParameterName="Image"Type="String"/><asp:ParameterName="Qty"Type="Int32"/><asp:ParameterName="PageNum"Type="Int32"/><asp:ParameterName="BinDing"Type="String"/><asp:ParameterName="Format"Type="Int32"/><asp:ParameterName="ISBN"Type="String"/><asp:ParameterName="Descn"Type="String"/><asp:ParameterName="Content"Type="String"/><asp:ParameterName="BookId"Type="Int32"/></UpdateParameters></asp:SqlDataSource>