Hi iam.hassan,
Check this example. Now please take its reference and correct your code.
HTML
<div>
<center>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hdID" Value='<%# Eval("CustomerId") %>' runat="server" />
<asp:Button ID="btnSweetAlert" runat="server" Text="Delete" CssClass="btn btn-primary" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</center>
</div>
<link media="screen" rel="stylesheet" href="https://cdn.jsdelivr.net/sweetalert2/6.3.8/sweetalert2.min.css" />
<link media="screen" rel="stylesheet" href='https://cdn.jsdelivr.net/sweetalert2/6.3.8/sweetalert2.css' />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/3.3.5/bluebird.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<script type="text/javascript" src="https://cdn.jsdelivr.net/sweetalert2/6.3.8/sweetalert2.min.js"> </script>
<script type="text/javascript">
$(function () {
$("[id*=btnSweetAlert]").on("click", function () {
var id = $(this).closest('tr').find('[id*=hdID]').val();
swal({
title: 'Are you sure?',
text: "You won't be able to revert this!",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes',
cancelButtonText: 'No',
confirmButtonClass: 'btn btn-success',
cancelButtonClass: 'btn btn-danger',
buttonsStyling: false
}).then(function (result) {
if (result) {
$.ajax({
type: "POST",
url: "Default.aspx/DeleteClick",
data: "{id:" + id + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
if (r.d == "Deleted") {
location.reload();
}
else {
swal("Data Not Deleted", r.d, "success");
}
}
});
}
},
function (dismiss) {
if (dismiss == 'cancel') {
swal('Cancelled', 'No record Deleted', 'error');
}
});
return false;
});
});
</script>
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT * FROM Customers";
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
[System.Web.Services.WebMethod]
public static string DeleteClick(int id)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "DELETE FROM Customers WHERE CustomerId = @Id";
SqlCommand cmd = new SqlCommand(query);
SqlConnection con = new SqlConnection(conString);
cmd.Connection = con;
con.Open();
cmd.Parameters.AddWithValue("@Id", id);
int i = cmd.ExecuteNonQuery();
con.Close();
return i > 0 ? "Deleted" : "Failed";
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Me.IsPostBack Then
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT * FROM Customers"
Dim cmd As SqlCommand = New SqlCommand(query)
Using con As SqlConnection = New SqlConnection(conString)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Using
End Using
End If
End Sub
<System.Web.Services.WebMethod()>
Public Shared Function DeleteClick(ByVal id As Integer) As String
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "DELETE FROM Customers WHERE CustomerId = @Id"
Dim cmd As SqlCommand = New SqlCommand(query)
Dim con As SqlConnection = New SqlConnection(conString)
cmd.Connection = con
con.Open()
cmd.Parameters.AddWithValue("@Id", id)
Dim i As Integer = cmd.ExecuteNonQuery()
con.Close()
Return If(i > 0, "Deleted", "Failed")
End Function
Screenshot