c# - Delete button is deleting random item although I have the right code -
i have grid view gets data database. has delete function , set delete row depending on productid. when try delete row, yes deleting row randomly. not 1 want delete.
here grid view:
<asp:gridview id="gdview" runat="server" autogeneratecolumns="false" datakeynames="productid" onrowcancelingedit="gdview_rowcancelingedit" onrowdeleting="gdview_rowdeleting" width="100%" cssclass="table table-hover table-striped" onrowdatabound="gdview_rowdatabound"> <columns> <asp:boundfield headertext="id" datafield="productid" sortexpression="productid" > <itemstyle height="20px" width="150px" /> </asp:boundfield> <asp:boundfield headertext="name" datafield="name" sortexpression="name" > <itemstyle height="20px" width="150px" /> </asp:boundfield> <asp:boundfield headertext="productcategory" datafield="categoryname" sortexpression="categorynaame" > <itemstyle height="20px" width="150px" /> </asp:boundfield> <asp:boundfield headertext="price" datafield="price" sortexpression="price" > <itemstyle height="20px" width="150px" /> </asp:boundfield> <asp:imagefield headertext ="imageurl" dataimageurlfield="imageurl" sortexpression="imageurl" controlstyle-width ="10"> <controlstyle width="50px"></controlstyle> </asp:imagefield> <asp:boundfield headertext="productquantity" datafield="productquantity" sortexpression="productquantity" > <itemstyle height="20px" width="150px" /> </asp:boundfield> <asp:boundfield headertext="productsold" datafield="productsold" sortexpression="productsold" > <itemstyle height="20px" width="150px" /> </asp:boundfield> <asp:boundfield headertext="availablestock" datafield="availablestock" sortexpression="availablestock" > <itemstyle height="20px" width="150px" /> </asp:boundfield> <%--<asp:commandfield showeditbutton="true"> <itemstyle width="100px" /> </asp:commandfield>--%> <asp:templatefield> <itemtemplate> <asp:linkbutton id="lnkdel" runat="server" text="delete" commandname="delete" onclientclick="return confirm('confirm delete?');"></asp:linkbutton> </itemtemplate> <itemstyle width="100px" /> </asp:templatefield> </columns> and here code behind. delete function:
protected void gdview_rowdeleting(object sender, gridviewdeleteeventargs e) { int prodid = int.parse(gdview.datakeys[0].value.tostring()); sqlconnection conn = new sqlconnection("data source = 'paulo'; initial catalog=shoppingcartdb;integrated security =true"); sqldataadapter da = new sqldataadapter("", conn); conn.open(); da.deletecommand = new sqlcommand("delete products productid=" + prodid, conn); da.deletecommand.executenonquery(); conn.close(); getproducts(0); }
change code
int prodid = convert.toint32(gdview.datakeys[e.rowindex].values["productid"].tostring()); from
int prodid = int.parse(gdview.datakeys[0].value.tostring());
Comments
Post a Comment