Hi narasiman,
Please refer below sample.
HTML
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CustomerId" HeaderText="CustomerId" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
<asp:Button Text="Submit" runat="server" OnClick="Select" />
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
gvCustomers.DataSource = GetData();
gvCustomers.DataBind();
}
}
private DataTable GetData()
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
}
}
}
protected void Select(object sender, EventArgs e)
{
DataTable dt = GetData();
for (int i = 0; i < gvCustomers.Rows.Count; i++)
{
CheckBox checkbox = (CheckBox)gvCustomers.Rows[i].Cells[0].FindControl("chk");
if (checkbox.Checked)
{
if (dt.Select("Name='" + gvCustomers.Rows[i].Cells[2].Text + "'").Length > 1)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "CropImage", "alert('" + gvCustomers.Rows[i].Cells[2].Text + " is duplicate.')", true);
break;
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
gvCustomers.DataSource = GetData()
gvCustomers.DataBind()
End If
End Sub
Private Function GetData() As DataTable
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Customers", con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Protected Sub [Select](ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = GetData()
For i As Integer = 0 To gvCustomers.Rows.Count - 1
Dim checkbox As CheckBox = CType(gvCustomers.Rows(i).Cells(0).FindControl("chk"), CheckBox)
If checkbox.Checked Then
If dt.[Select]("Name='" & gvCustomers.Rows(i).Cells(2).Text & "'").Length > 1 Then
ScriptManager.RegisterStartupScript(Me, Me.[GetType](), "CropImage", "alert('" & gvCustomers.Rows(i).Cells(2).Text & " is duplicate.')", True)
Exit For
End If
End If
Next
End Sub
Screenshot