Validation to Check Duplicate Records Before Inserting Using in Asp.Net C#

Validation to Check Duplicate Records Before Inserting


If UserName or Id Values Enter in  Manually  Avoid Duplicate Values Before Insert  to Validate Using  Asp.Net C#.


                                                        DEMO


   DOWNLOAD CODING



                          HTML CODING



<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
      

        Username
        <asp:TextBox ID="txtUsername" runat="server" AutoPostBack="True"

            OnTextChanged="txtUsername_TextChanged" ></asp:TextBox>

        <asp:Label ID="Label1" runat="server" ForeColor="#CC3300" Visible="False"></asp:Label>
     


    </div>
    </form>
</body>
</html>

                        
                     C# CODING

  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Net;


public partial class Login : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter adp;
    SqlDataReader rd;  
    string query;


    public void dbcon()
    {
        string connn = (System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
        con = new SqlConnection(connn);
        con.Open();

    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected void txtUsername_TextChanged(object sender, EventArgs e)
    {
        dbcon();
        query = "select username from register";
        cmd = new SqlCommand(query, con);       
        rd = cmd.ExecuteReader();
        while(rd.Read())
        {
            Label1.Text = "";

            if(rd["username"].ToString() ==                                txtUsername.Text.ToString())

            {
                Label1.Visible = true;

                Label1.Text = "Already Exists";

                break;
           
            }
         
        }
    }
}




First Create New WebForm - Add the Label & Textbox Property Set AutuPostBack =True








Next - Label - Property Set Visible=True








Showing Html Coding








Next - DataBase - Table  -  Show The Existing Values








Next - Double Click - TextBox  - Add NameSpaces & DtaBase Connection







Double Click - TextBox  - Add The Coding 








Next - Run The Program - Check The Existing Values  
OUTPUT











3 comments:

  1. protected void Txtemail_TextChanged(object sender, EventArgs e)
    {

    string connn = (System.Configuration.ConfigurationManager.ConnectionStrings["cn"].ToString());
    SqlConnection con = new SqlConnection();

    con.Open();
    string query;
    query = "select username from register";
    SqlCommand cmd = new SqlCommand(query, con);
    SqlDataReader dr;
    dr = cmd.ExecuteReader();
    while (dr.Read())
    {
    lblmessage.Text = "";

    if (dr["Mobile no"].ToString() == Txtmobile.Text.ToString())

    {
    lblmessage.Visible = true;

    lblmessage.Text = "Already Exists";

    break;

    }


    it is not working

    ReplyDelete