Click here to Skip to main content
15,917,926 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
what code should be written so that same data should not be inserted again in database.
Posted
Comments
CHill60 28-Apr-13 15:44pm    
This question is not clear. Use the Improve question link to add more detail - for example the line(s) of code that you are using to insert data into the database
PIEBALDconsult 28-Apr-13 16:22pm    
You probably want to look into "referential integrity", "unique index", and "primary key".

There are different approaches depending on what you are trying to do;

1) Mark the database schema such that fields must be unique, are primary keys etc.

2) Build a hash from the data and store that with the data as a field. When you create a new record calculate the hash and try and save the new data. The hash field must be marked unique.

3) Come up with your own method of checking for uniqueness.
 
Share this answer
 
Suppose your database contains field called username,then you can check if username exist in the database or not,if not then enter the details else throw an exception.Example is given below.

C#
SqlConnection conn = new SqlConnection();
 conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["connection string name"].ConnectionString;
 conn.Open();
 String user=TextBox1.Text;
 SqlCommand check = new SqlCommand("select * from table_name where name=@user", conn);
 cmd.Parameters.Add(new SqlParameter("@user",user));
 SqlDataReader reader = check.ExecuteReader();
 if (!reader.Read())
 {
        /*Insert details into database*/
 }
 else
 {
       /*Throw en Exception*/
 }
 
Share this answer
 
v3

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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