<asp:GridView EnableViewState="true" DataKeyNames="ID" style="width:100%;" ID="tableUsers" runat="server" CssClass="table table-striped table-bordered" AllowSorting="True" AllowPaging="True" OnPageIndexChanging="tableUsers_PageIndexChanging" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" OnRowDeleting="tableUsers_RowDeleting" OnRowEditing="tableUsers_RowEditing" AutoGenerateColumns="False" OnRowCancelingEdit="tableUsers_RowCancelingEdit" OnRowUpdating="tableUsers_RowUpdating" OnSorting="tableUsers_Sorting">
<Columns>
<asp:TemplateField HeaderText="ID" Visible="False">
<ItemTemplate>
<asp:Label ID="txtID" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Username">
<EditItemTemplate>
<asp:TextBox ID="txtUsername" runat="server" Text='<%# Eval("Username") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Username") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="deleteBox" runat="server" Enabled="true"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
tableFill();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
try
{
bool checkedfound = false;
foreach (GridViewRow grow in tableUsers.Rows)
{
//Searching CheckBox("chkDel") in an individual row of Grid
CheckBox chkBx = (CheckBox)grow.FindControl("deleteBox");
//If CheckBox is checked than delete the record with particular empid
if (chkBx != null && chkBx.Checked)
{
string id = tableUsers.DataKeys[grow.RowIndex].Values["ID"].ToString();
master.execute("DELETE FROM Users WHERE ID='" + id + "'", "User Deleted", "ERROR");
checkedfound = true;
}
}
if(checkedfound){
alert("Selected users deleted", true);
tableFill();
}
else
{
alert("no checks found",false);
}
}
catch (Exception x)
{
alert("Delete Fail: "+x.ToString(), false);
}
}
protected void tableUsers_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
string id = tableUsers.DataKeys[e.RowIndex].Values["ID"].ToString();
master.execute("DELETE FROM Users WHERE ID='" + id + "'", "User Deleted", "ERROR");
alert("User deleted",true);
tableFill();
}
catch (Exception)
{
alert("Delete Failed",false);
}
}
private void tableFill()
{
string constr = System.Configuration.ConfigurationManager.ConnectionStrings["WebAppConnString"].ToString();
using (MySqlConnection con = new MySqlConnection(constr))
{
using (MySqlCommand cmd = new MySqlCommand("SELECT ID,Username,Email,Rank,IP FROM Users"))
{
using (MySqlDataAdapter sda = new MySqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
tableUsers.DataSource = dt;
tableUsers.DataBind();
}
}
}
}
}
Trying to delete multiple rows upon Buttob2 click. However, i keep recieving the error of "no checks found". Ive tried adding the !postback in the load but still no luck. I have also coppied others code on the same issue and the only responses are to add the !postback
(The gridview loads the data fine and tableUsers_RowDeleting works)