<
script language="javascript" type="text/javascript"> function ConfirmDelete(){
return confirm("Are u sure u want to delete all these records?")}
script>
<
asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" DataKeyNames="CustomerID"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:CheckBox ID="chkgrid" runat="server"/> ItemTemplate> asp:TemplateField> <asp:BoundField DataField="CustomerID" Visible="false"/> <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" /> <asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" /> <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" /> Columns> asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbPDMS_V1.5ConnectionString6 %>" SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName], [Country] FROM [Customer]" DeleteCommand="Delete from [Customer] where [CustomerID]=@Customerid"> <DeleteParameters> <asp:Parameter Name="CustomerID"/> DeleteParameters> asp:SqlDataSource> <asp:Button ID="btndelete" Text="delete" runat="server" OnClick="btndelete_click" OnClientClick="return ConfirmDelete()"/>
code behind:
using
System;using
System.Data;using
System.Configuration;using
System.Collections;using
System.Web;using
System.Web.Security;using
System.Web.UI;using
System.Web.UI.WebControls;using
System.Web.UI.WebControls.WebParts;using
System.Web.UI.HtmlControls;public
partial class batchDelete : System.Web.UI.Page{
protected void Page_Load(object sender, EventArgs e){
}
protected void btndelete_click(object sender ,EventArgs e){
foreach(GridViewRow row in GridView1. Rows){
CheckBox cb = (CheckBox)row.FindControl("chkgrid"); if(cb.Checked){
int customerid = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);SqlDataSource1.DeleteParameters[
"CustomerID"].DefaultValue = customerid.ToString();SqlDataSource1.Delete();
}
}
}
}
jignesh kadvaniPosted Sep 30, 2008, 8:36 AM
Index was out of range. Must be non-negative and less than the size of the collection.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Parameter name: index
Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Source Error:
Line 47: if (chkBox.Checked) Line 48: { Line 49: int id = Convert.ToInt32(gvEmployee.DataKeys[objData.RowIndex].Value.ToString()); Line 50: SqlCommand cmd = new SqlCommand(); Line 51: cmd.Connection = con;i use below code for delete .
protected void btnDelete_Click(object sender, EventArgs e)
{
foreach (GridViewRow objData in gvEmployee.Rows)
{
CheckBox chkBox = (CheckBox)objData.FindControl("chkDelete");
if (chkBox.Checked)
{
int id = Convert.ToInt32(gvEmployee.DataKeys[objData.RowIndex].Value);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText="delete from Emp where Eid="+id;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
bind();
}
// gvEmployee.EditIndex=-1; // i tried this also but didn't worked.
// bind();
}
}
shakti parmarPosted Jul 9, 2008, 6:49 AM
hi
iam shakti I want to show multipal image in the gridview from the database and inside the each image checkbox for delete , update, select if i checked the delete checkbox and click the delete_link_button delete my image from the database if I select check the select_checkbox show image in the new windows if i check the my update_linkbutton i can update or replace new image on the old image please tell me how can this possible