Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to check if value inserted in a text box is duplicate or not before clicking on submit button

I tried the following code but I failed doing so

C#
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;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(@"Data Source=NEENA-PC\SQLEXPRESS;Initial Catalog=practice;Integrated Security=True");
    
    public void custom()
    {
        string select = "Select name from [insert]";
        SqlCommand cmd=new SqlCommand(select,con);
        SqlDataReader sdr;
        con.Open();
        sdr = cmd.ExecuteReader();
        //if (!IsPostBack)
        //{
            if (sdr.Read())
            {
                if (TextBox1.Text == sdr[0].ToString())
                {
                    Label1.Text = "error";
                    TextBox1.Text = "";
                    TextBox1.Focus();
                    //CustomValidator1.ErrorMessage = "erroe";
                }
                else
                {
                    Label1.Text = "ok";
                    //CustomValidator1.ErrorMessage = "scd";
                }
            }
       // }
        con.Close();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        custom();
        Response.Write("Hiii");
    }
}


Thanks.
Posted
Comments
Mike Meinz 19-Mar-13 9:23am    
Your SELECT statement doesn't have a Where clause. It will select all name values in the [insert] table.
NeenaM 19-Mar-13 13:03pm    
I want all names from the table so why should I use where caluse
Mike Meinz 19-Mar-13 14:12pm    
You said that you wanted to check if the value entered by the user is a duplicate of what is in the database. To do that, you use a SELECT statement with a WHERE clause to see if there is a row in the database table that is the same value as the value entered by the user. To read all rows from the database table to check if one matches the value entered by the user is not considered good practice. It is very wasteful of computer resources and the user's time.
NeenaM 19-Mar-13 14:52pm    
ok I understood thanks for the help.
Good solution
CHill60 19-Mar-13 9:24am    
Please use the Improve question link to explain what the failure was

Just issue a query with following content, and supplying the string to look for as variable: Select * from [insert] where name=@name. If you get any non-empty result, you have the name already.
You can make an ajax callback to check this from client side.
 
Share this answer
 
v2
Comments
NeenaM 19-Mar-13 13:04pm    
how to use ajax callback
Zoltán Zörgő 19-Mar-13 13:29pm    
Read this the two tutorials here: http://ajax.asp.net/docs/tutorials/ASPNETAJAXWebServicesTutorials.aspx
NeenaM 19-Mar-13 14:54pm    
Thanks
Try like this

System.Collections.IEnumerator ir = ds.Tables[0].Rows.GetEnumerator();

while (ir.MoveNext())
{
DataRow dr = ir.Current as DataRow;
TextBox1.Text = dr["Status"]
}
 
Share this answer
 
v2
Comments
NeenaM 19-Mar-13 13:06pm    
I am not able to understand this code. Can you explain me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900