Hi srihitha,
Check this example. Now please take its reference and correct your code.
Database
I have made use of the following table Customers with the schema as follows.

I have already inserted few records in the table.
You can download the database table SQL by clicking the download link below.
Download SQL file
HTML
<asp:ScriptManager runat="server" EnablePageMethods="true" />
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:LinkButton ID="btnDelete" Text="Delete" CommandArgument='<%#Eval("CustomerId")%>'
CommandName="rowDelete" runat="server" OnClientClick="if(!SubUnitDeleteErrorWarning_SelectedItem(this)) return false;" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CustomerId" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
<script type="text/javascript">
function SubUnitDeleteErrorWarning_SelectedItem(lnk) {
var row = lnk.parentNode.parentNode;
var rowIndex = row.rowIndex - 1;
var country = row.cells[3].innerHTML;
PageMethods.GetSubUnitDeleteWarningByID(country, onSuccess, onError);
function onSuccess(result) {
if (result > 0) {
if (confirm("Do you want to delete this subunit so " + result + " travelers will be affected do you want to continue still?")) {
PageMethods.DeleteSubUnitByID(country, function () {
alert(result + " Travelers deleted.");
window.location.reload();
}, onError);
}
}
else {
alert("No Travelers will be affected if you delete this subunit.")
return false;
}
}
function onError(err) {
alert(err);
}
}
</script>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Services
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
GridView1.DataSource = GetData();
GridView1.DataBind();
}
}
private static DataTable GetData()
{
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);
return dt;
}
}
}
}
[WebMethod]
public static string GetSubUnitDeleteWarningByID(string country)
{
return GetData().Select("Country='" + country + "'").Length.ToString();
}
[WebMethod]
public static string DeleteSubUnitByID(string country)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "DELETE FROM Customers WHERE Country = @Country";
SqlConnection con = new SqlConnection(conString);
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@Country", country);
con.Open();
int count = cmd.ExecuteNonQuery();
con.Close();
return count > 0 ? "1" : "0";
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
GridView1.DataSource = GetData()
GridView1.DataBind()
End If
End Sub
Private Shared Function GetData() As DataTable
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)
Return dt
End Using
End Using
End Using
End Function
<WebMethod()>
Public Shared Function GetSubUnitDeleteWarningByID(ByVal country As String) As String
Return GetData().Select("Country='" & country & "'").Length.ToString()
End Function
<WebMethod()>
Public Shared Function DeleteSubUnitByID(ByVal country As String) As String
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "DELETE FROM Customers WHERE Country = @Country"
Dim con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand(query, con)
cmd.Parameters.AddWithValue("@Country", country)
con.Open()
Dim count As Integer = cmd.ExecuteNonQuery()
con.Close()
Return If(count > 0, "1", "0")
End Function
Screenshot
