In this article I will explain with an example, how to check for duplicate values while inserting in GridView without SQL in ASP.Net using C# and VB.Net.
When the Add Button is clicked, the values of TextBoxes will be compared with the Row values of the GridView using ASP.Net CustomValidator and JavaScript and if the values are duplicate, error messages will be displayed.
 
 
Database
I have made use of the following table Customers with the schema as follows.
Check for Duplicate values while inserting in GridView without SQL in ASP.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 

HTML Markup
The HTML Markup consists of an ASP.Net GridView with three BoundField columns.
Below the GridView there’s a Form with two TextBoxes, a Button for adding row (data) to the GridView and the Database and a CustomValidator for duplicate check validation.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" EmptyDataText="No records has been added." Width="450">
    <Columns>
        <asp:BoundField DataField="CustomerId" HeaderText="Customer Id" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
    </Columns>
</asp:GridView>
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse;
    width: 450px">
    <tr>
        <td style="width: 150px">
            Name:<br />
            <asp:TextBox ID="txtName" runat="server" Width="140" />
        </td>
        <td style="width: 150px">
            Country:<br />
            <asp:TextBox ID="txtCountry" runat="server" Width="140" />
        </td>
        <td style="width: 150px">
            <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Insert" />
        </td>
    </tr>
</table>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="" ForeColor="Red" ClientValidationFunction="CheckDuplicates"></asp:CustomValidator>
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
 
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
 
 
Binding the GridView with records from SQL Database Table
The GridView is populated from the database inside the Page Load event of the page.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.BindGrid();
    }
}
 
private void BindGrid()
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    string query = "SELECT * FROM Customers";
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter(query, con))
        {
            using (DataTable dt = new DataTable())
            {
                sda.Fill(dt);
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Me.BindGrid()
    End If
End Sub
 
Private Sub BindGrid()
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim query As String = "SELECT * FROM Customers"
    Using con As SqlConnection = New SqlConnection(constr)
        Using sda As SqlDataAdapter = New SqlDataAdapter(query, con)
            Using dt As DataTable = New DataTable()
                sda.Fill(dt)
                GridView1.DataSource = dt
                GridView1.DataBind()
            End Using
        End Using
    End Using
End Sub
 
 
Inserting records to GridView
The following event handler is executed when the Add Button is clicked. The Name and Country values are fetched from their respective TextBoxes and then passed to the SQL Query for inserting the record in the database.
Finally, the GridView is again populated with data by making call to the BindGrid method.
C#
protected void Insert(object sender, EventArgs e)
{
    string name = txtName.Text;
    string country = txtCountry.Text;
    txtName.Text = "";
    txtCountry.Text = "";
    string query = "INSERT INTO Customers VALUES(@Name, @Country)";
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand(query))
        {
            cmd.Parameters.AddWithValue("@Name", name);
            cmd.Parameters.AddWithValue("@Country", country);
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
    this.BindGrid();
}
 
VB.Net
Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs)
    Dim name As String = txtName.Text
    Dim country As String = txtCountry.Text
    Dim query As String = "INSERT INTO Customers VALUES(@Name, @Country)"
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    txtName.Text = ""
    txtCountry.Text = ""
    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand(query)
            cmd.Parameters.AddWithValue("@Name", name)
            cmd.Parameters.AddWithValue("@Country", country)
            cmd.Connection = con
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
        End Using
    End Using
 
    Me.BindGrid()
End Sub
 
 
Check for Duplicate values while inserting in GridView without SQL
The following JavaScript function will be called when the Add Button is clicked. Inside this JavaScript function, a loop will be executed over the GridView Rows and the Cell values will be compared with the TextBox values.
And if the values are duplicate, error messages will be displayed.
<script type="text/javascript">
    function CheckDuplicates(sender, args) {
        //Reference the GridView.
        var grid = document.getElementById("<%=GridView1.ClientID %>");
 
        //Reference all Rows of the GridView.
        var rows = grid.getElementsByTagName("TR");
        for (var i = 1; i < rows.length; i++) {
 
            //Reference the Cells of the Row.
            var cells = rows[i].getElementsByTagName("TD");
 
            //Fetch the values from the Cells.
            var name = cells[1].innerHTML;
            var country = cells[2].innerHTML;
 
            //Compare the values and check for Duplicates.
            if (name == document.getElementById("<%=txtName.ClientID %>").value.trim()) {
                sender.innerHTML = "Duplicate Name";
                args.IsValid = false;
                return;
            }
            if (country == document.getElementById("<%=txtCountry.ClientID %>").value.trim()) {
                sender.innerHTML = "Duplicate Country";
                args.IsValid = false;
                return;
            }
        }
 
        args.IsValid = true;
    }
</script>
 
 
Screenshot
Check for Duplicate values while inserting in GridView without SQL in ASP.Net
 
 
Downloads