Hi david.ee.
Check this example. Now please take its reference and correct your code.
For this example I have used of NorthWind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:GridView ID="gvCustomer" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="CompanyName">
<ItemTemplate>
<asp:TextBox ID="txtCompany" runat="server" Text='<%# Eval("CompanyName") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:TextBox ID="txtCountry" runat="server" Text='<%# Eval("Country") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Is Exist">
<ItemTemplate>
<asp:Label ID="lblExist" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnCheck" runat="server" Text="Check" OnClick="btnCheck_Click" />
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)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("CompanyName", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt.Rows.Add();
dt.Rows.Add();
gvCustomer.DataSource = dt;
gvCustomer.DataBind();
}
}
protected void btnCheck_Click(object sender, EventArgs e)
{
string str = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
for (int i = 0; i <= gvCustomer.Rows.Count - 1; i++)
{
string companyName = (gvCustomer.Rows[i].FindControl("txtCompany") as TextBox).Text;
string country = (gvCustomer.Rows[i].FindControl("txtCountry") as TextBox).Text;
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT COUNT(CustomerID) FROM Customers WHERE CompanyName = @CompanyName AND Country = @Country";
cmd.Parameters.AddWithValue("@CompanyName", companyName);
cmd.Parameters.AddWithValue("@Country", country);
cmd.Connection = con;
con.Open();
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count > 0)
{
(gvCustomer.Rows[i].FindControl("lblExist") as Label).Text = "Record already exists in the database";
}
else
{
(gvCustomer.Rows[i].FindControl("lblExist") as Label).Text = "Record not exists in the database";
}
con.Close();
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(1) {New DataColumn("CompanyName", GetType(String)), New DataColumn("Country", GetType(String))})
dt.Rows.Add()
dt.Rows.Add()
gvCustomer.DataSource = dt
gvCustomer.DataBind()
End If
End Sub
Protected Sub btnCheck_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim str As String = ConfigurationManager.ConnectionStrings("ConString").ConnectionString
For i As Integer = 0 To gvCustomer.Rows.Count - 1
Dim companyName As String = (TryCast(gvCustomer.Rows(i).FindControl("txtCompany"), TextBox)).Text
Dim country As String = (TryCast(gvCustomer.Rows(i).FindControl("txtCountry"), TextBox)).Text
Dim con As SqlConnection = New SqlConnection(str)
Dim cmd As SqlCommand = New SqlCommand()
cmd.CommandText = "SELECT COUNT(CustomerID) FROM Customers WHERE CompanyName = @CompanyName AND Country = @Country"
cmd.Parameters.AddWithValue("@CompanyName", companyName)
cmd.Parameters.AddWithValue("@Country", country)
cmd.Connection = con
con.Open()
Dim count As Integer = Convert.ToInt32(cmd.ExecuteScalar())
If count > 0 Then
TryCast(gvCustomer.Rows(i).FindControl("lblExist"), Label).Text = "Record already exists in the database"
Else
TryCast(gvCustomer.Rows(i).FindControl("lblExist"), Label).Text = ("Record not exists in the database")
End If
con.Close()
Next
End Sub
Screenshot
