This is a common problem but is easy to fix..
- Change your user table so that the userid is an identity column
- When you insert your new user details into this table a new unique userid will automatically created for you by SQLServer
- To obtain the userid that has been created for you, use the SCOPE_IDENTITY() function
For more information on identity columns and how to retrieve your new userid, have a look at this tutorial..
SQLTeam - Understanding Identity Columns
Edit (based on comment below)
The reason you are currently getting duplicate user ids is because you have a time gap between requesting the max(userid) and updating it/inserting your new user record.
It doesn't matter if this delay (between request and update) is 10 minutes or 10 milliseconds, if your site is being hit hard enough the problem will still occur.
By using identity columns, SQLServer effectively removes the delay for you by creating the id for you. No other process/user can be given the same userid.
Try with the following code...
/*Create Table*/
CREATE TABLE TestTableSO (UserID INT IDENTITY(1,1), Username NVARCHAR(50))
/*Insert some data, note that I'm not providing a UserID on my Insert*/
INSERT INTO TestTableSO (Username) VALUES ('Joe')
INSERT INTO TestTableSO (Username) VALUES ('Dan')
INSERT INTO TestTableSO (Username) VALUES ('Fred')
INSERT INTO TestTableSO (Username) VALUES ('Sam')
/*Look at the data I've inserted*/
SELECT * FROM TestTableSO
/*Insert one more record*/
INSERT INTO TestTableSO (Username) VALUES ('Roger')
/*Look at the new UserID I have been given by SQL Server*/
SELECT SCOPE_IDENTITY() as NewUserID
/*Drop our table, only needed for testing*/
DROP TABLE TestTableSO
From this code you will get the following output...
UserID Username
------------------
1 Joe
2 Dan
3 Fred
4 Sam
NewUserID
-----------
5
Hope this makes sense! Again, to repeat. You will not solve this problem using Max(UserID)
in the way you are.
Edit #2
Creating UserIDs without storing any information to go with the UserID is a BAD idea. You risk running out of available IDs much sooner because of users that will access your site, get given a userid but then not fill in any details. Only provide them with a userid if they are a true user.