Delete function not working if check box is unchecked so the alert message not poping up when the checkbox is unchecked .
Database Structure
CREATE TABLE [dbo].[order]( [id] [int] IDENTITY(1,1) NOT NULL, [accepted] [nvarchar](255) NULL, ) ON [PRIMARY] GO
CREATE TABLE [dbo].[Tracking]( [id] [int] IDENTITY(1,1) NOT NULL, [cylinderid] [nvarchar](255) NULL, [cylinderno] [nvarchar](255) NULL ) ON [PRIMARY] GO
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#3366CC" BorderStyle="None"
Font-Names="Century Gothic" Font-Size="x-Small" DataKeyNames="id" Width="38%" Style="margin-bottom: 0px" CssClass="grid">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" OnCheckedChanged="OnChckedChanged" Text="Add" AutoPostBack="True" EnableViewState="False" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id" HeaderText="id" />
<asp:BoundField DataField="accepted" HeaderText="Cylinder No" />
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#7EA9D3" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000000" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="#000000" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindUserDetails();
}
}
protected void BindUserDetails()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter("Select id,purchaseorderno, accepted from orders ORDER BY id DESC", con))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
gvDetails.DataSource = dt;
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
}
}
//Required for jQuery DataTables to work.
gvDetails.UseAccessibleHeader = true;
gvDetails.HeaderRow.TableSection = TableRowSection.TableHeader;
}
protected void OnChckedChanged(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
GridViewRow row = (GridViewRow)chk.NamingContainer;
string id = row.Cells[1].Text;
string number = row.Cells[2].Text;
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
if (chk.Checked)
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Tracking VALUES(@Id, @CylinderNo)", con))
{
cmd.Parameters.AddWithValue("@Id", id);
cmd.Parameters.AddWithValue("@CylinderNo", number);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted successfully.')", true);
}
else
{
using (SqlCommand cmd = new SqlCommand("DELETE FROM Tracking WHERE cylinderid = @Id", con))
{
cmd.Parameters.AddWithValue("@Id", id);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Deleted successfully')", true);
}
}
}