Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 7th, 2007, 02:33 PM
enak enak is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2007
Posts: 2 enak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 20 sec
Reputation Power: 0
Duplicate entries

I have an application that allows the user to enter data into a table. There are multiple users so I put in some code in the frontend that, I thought, would keep 2 users from creating a new record at the same time. The IDs for the records are identical and this is causing a problem.

The IDs are in the format of ####-mmyy. at the start of each month the #### part goes back to 1. Because of the format of the ID I can not make it an identity field.

We tried a test today where we had 2 users click on the New button at exactly the same time. The IDs that were created were identical. Is there anyway on the database that I can prevent this from happening?

Here is how I create the new record id:

I get the MAX(ID) from the table from the last record created
I add 1 to the ID and then insert a new record with the new ID into the table.

But what is happening is that when the 2 users try to add a new record both users are grabbing the last ID before the first user to execute the query can insert a new ID.

For example:

Current last record = 1

User1 and User2 click New. The system goes to the table and both users get ID = 1. The system then adds 1 to the ID (1 + 1 = 2) and inserts and new ID of 2 for User1 and a new ID for User2.

I know that they are not getting to the table at the same time so what I need is for the first user to start the process to be able to lock the second user out until the first user has inserted a new record. That way the second user will get the new ID as the current ID.

Currently, I am not using stored procedures to do this. Should I?

I really hope that this makes sense.


Any help is appreciated.

Thanks,
enak

Reply With Quote
  #2  
Old February 8th, 2007, 04:18 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 997 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 904898 Folding Title: Super Ultimate Folder - Level 2Folding Points: 904898 Folding Title: Super Ultimate Folder - Level 2Folding Points: 904898 Folding Title: Super Ultimate Folder - Level 2Folding Points: 904898 Folding Title: Super Ultimate Folder - Level 2Folding Points: 904898 Folding Title: Super Ultimate Folder - Level 2Folding Points: 904898 Folding Title: Super Ultimate Folder - Level 2Folding Points: 904898 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 14 h 26 m 27 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
The proper way to do this is have the program obtain a read lock on the table first. That way, only one thread can read or write the table. It will read the last record, store the new one, and then it will have to release the lock. The second thread will have to wait till the first one is finished, and will see the new record as the last one.

Have a look here as well.
__________________
This is my code. Is it not nifty?

"The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools."
---Douglas Adams


Join the Itsacon fanclub!    
Zero Tolerance: Spammers banned so far: 280

Reply With Quote
  #3  
Old February 8th, 2007, 10:51 AM
enak enak is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2007
Posts: 2 enak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 20 sec
Reputation Power: 0
Quote:
Originally Posted by Itsacon
The proper way to do this is have the program obtain a read lock on the table first. That way, only one thread can read or write the table. It will read the last record, store the new one, and then it will have to release the lock. The second thread will have to wait till the first one is finished, and will see the new record as the last one.

Have a look here as well.


This sounds like what I am looking for. However, I have searched high and low and can not find any info on HOW to do this. Can you please provide an example on how to do this? Do I do it in my vb.net code?

Thanks for the help.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft SQL Server > Duplicate entries


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT