473,395 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

how to avoid duplicate ids to store in database

66
hi friends...

i am inserting Employee id to database through form...
how to find that the id is already there in the database.. if its already present then one message should come .. and the Emp id should not store in the database...
how to do this

i am using sql server 2005, C# and Asp.net

i have assigned the primary key to Emp Id...
but i also want to write a code in the application
Sep 19 '08 #1
4 2128
kunal pawar
297 100+
I suppose not need to do more. Coz ur field is primary so it not allowed to insert duplicate ID record.

When u try to insert duplicate ID it throws exception. U hv to catch it and display message.
duplicate key not allowed
Sep 19 '08 #2
cloud255
427 Expert 256MB
If you want to do it in code you need to select all the IDs from your table when the user clicks to submit.

Store these IDs in a datastructure, somethine like a binary search tree or hash table, then search for the ID the user has entered, if it is found show an error message. If it is not found simply perform the INSERT SQL command.
Sep 19 '08 #3
Write a condition like this before saving

DataTable dt = new DataTable();

execute that query like
" select Emp Id form table whre Emp Id=' Collect it form textbox control' "

save the data in 'dt' table


if(dt.rows.count > 0 )
error message "ID already exist
else
Write the remaining logic for insertion
Sep 19 '08 #4
rekhasc
66
Write a condition like this before saving

DataTable dt = new DataTable();

execute that query like
" select Emp Id form table whre Emp Id=' Collect it form textbox control' "

save the data in 'dt' table


if(dt.rows.count > 0 )
error message "ID already exist
else
Write the remaining logic for insertion



Thank for your replys....
actually i am taking the id from one table that displays in datagrid and inserting to other table.. while inserting to other table it should not take the duplicate values
Sep 20 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Mark | last post by:
When my form goes to a new record, I have a procedure that copies the last record added to the form's underlying table into the form. The intent is that a series of new records may have the same...
19
by: Charles Law | last post by:
Take a solution with a project hierarchy along the lines of an n-tier system, so that we have a data layer, business layer and presentation layer. The presentation layer is coupled to the business...
10
by: Noozer | last post by:
I'm writing an ASP application and have a noob question... I have a class that access an MS SQL database. I have another class also accesses an MS SQL database and this second class uses objects...
6
by: planetthoughtful | last post by:
Hi All, I have a C# ASP.NET page that submits back to itself to insert details from a form into a database table. When / if the user refreshes the page (and gets the standard warning that POST...
0
by: B.N.Prabhu | last post by:
Hi, I have a DataTable with several rows. Its having 20 Columns. when i click the Insert button then i have to check the Database Rows. Whether these new rows are already available in the...
5
by: ramab | last post by:
Hi. There are two fields in my table that i check so that both of them doesnt occur together again to create a duplicate record. My problem is that if one of the field loses focus then i am able to...
5
by: mercea | last post by:
hi all, How do i avoid duplicate records on my database? i have 4 textboxes that collect user information and this information is saved in the database. when a user fills the textboxes and clicks...
0
by: halex | last post by:
Hello, I am having deadlock problem when I have a lot of visitors on my website at the same time. I am using NetTiers templates to generate C# classes for accessing DB layer and problem is in my...
3
by: smugcool | last post by:
HI, I am having a database in access. Where user imports various data which is in excel format. I want them to stop importing any duplicate record. If any duplicate record is been trying to import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.