Solved

Preventing identical records

Posted on 2006-11-01
6
219 Views
Last Modified: 2010-04-16
Hi!

I would like the users on my website to be able to change their username to anything they desire. But how do I prevent identical usernames?

I know how to update a database, but how to check for identical records, thats the thing!

Any help highly appreciated!!

Cheers

Michael
Copenhagen
Denmark
0
Comment
Question by:micamb
6 Comments
 
LVL 35

Expert Comment

by:Raynard7
ID: 17848597
Hi,

If the field that you have the username stored in as a unique key - if you try to insert a duplicate record then you will get an error message if it is duplicated - so if you capture this error then you will know it already exists.
0
 
LVL 1

Expert Comment

by:TerryInOhio
ID: 17849236
Hello,

To make sure they did not choose the same user name as another person, do a querry on the database if you come back with 0 records, no one has it and it is safe to insert.  If you come back with a record then put up a message that it is in use.
0
 
LVL 4

Expert Comment

by:Ariful Ambia
ID: 17852840
HI
      U don’t tell what database ur using. If you use MS SQL server u can follow the steps below.
1. When u create unique constraint u will get a constraint name say “IX_TblUser”

2. Put your Update code in Try catch. When the Try catch got any error due to violation of the constraint “IX_TblUser” it will automatically give u a error with description that the “IX_TblUser” constraint has been violated so this transaction can’t be execute.

3. Create a function (say ErrorEncodedDesc(ErrorDes as String) AS string) which will parse the error description that is given by the database and look for predefine constraint names to match. And when get a match a more user friendly message will return for the function. For example when the “IX_TblUser” constraint violated the error description will pass into the ErrorDes parameter of the function ErrorEncodedDesc. The function will then look for the constraint IX_TblUser in the error description string. When it finds a match the function will return a clearer message to the user say (User name already exists.)

4. After giving the Clear message to the user that the Update failed due to this and this, u don’t have to anything farther coz database already reject the Update.

Hope it will help u.


Noman
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:jay_gadhavi
ID: 17855682
Example:

after putting value in username and password,
do the following steps,
1> check in the database weather ther username is exists or not.
       like : select * from user where username='username';
.
2>Fire above quary and bind the recordset or datatable
      If recordset or datatable length is greater then 0 then username exists in the database and put the appropriate message.
      IF recordset or datatable length is 0 then username not exists in the database , so u can allow this username




0
 

Author Comment

by:micamb
ID: 17855861
Thanx for all the quick responses!

I'm using access at the moment. How would the query look doing that?

Cheers

Michael
0
 
LVL 1

Accepted Solution

by:
TerryInOhio earned 125 total points
ID: 17857755
I use Access too.

Public cnnTig As ADODB.Connection
dim strUserNameEntered as String
Public Const DB_PROVIDER = "Microsoft.Jet.OLEDB.4.0"
Public Const StrDataBaseDirectory = "C:\Benefits"
Public Const DB_NAME = "BENEFITS.MDB"

strConnectionString = "Provider=" & DB_PROVIDER & ";Data Source=" & StrDataBaseDirectory & "\" & DB_NAME

cnnTig.Open strConnectionString
strSQL = "SELECT * FROM UsersTable where UserName = '" &  strUserName & "'"
RSwork.Open strSQL, cnnTig, adOpenStatic, adLockOptimistic, adCmdText
If RSwork.RecordCount < 1 Then
       RSwork.AddNew
       RSwork.Fields("UserName").Value = strUserName
       RSwork.Update
       Msgbox "Use Id " & strUserName & " has been added to the database".
else
       Msgbox "Use Id " & strUserName & " Already Exists in the database".
end if
Set RsWork = Nothing
cnnTig.Close
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now