Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There is one database name as "email_book" and in that database there is one table named as "contact_book"....name and mail_id are the two column which i had been created in that table.......now i want to add data into to my table using form in C# but on the counter part i just want to avoid inserting the repeated data into the table.......when user add same mail_id which is already present in the table then it must gives the message that "this mail_address is already present in database"........so for that i need coading in C#

.....please help its urgent
Posted
Updated 12-Feb-14 21:22pm
v2

You can count the value of data that is being inserted like this:

string constr = ConfigurationManager.ConnectionStrings["connstr"].ToString();
        SqlConnection con = new SqlConnection(constr);
        string sql1 = "SELECT COUNT (client_id) FROM client WHERE client_id = '" + txtid.Text + "' ";
        SqlCommand cmd = new SqlCommand(sql1, con);
        con.Open();
        int temp = Convert.ToInt32(cmd.ExecuteScalar().ToString());
       if (temp >0)
        {
//show error message
        }
 
Share this answer
 
Comments
Gaurav Manusmare 13-Feb-14 11:59am    
Thank you sir..............you provide me accurate code for which i have been looking for.............thanx a lot sir
set mail_id column as IDENTITY. that prevents duplicated key usage.

http://msdn.microsoft.com/en-us/library/ms186775.aspx[^]
 
Share this answer
 

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