Solved

Preventing identical records

Posted on 2006-11-01
6
218 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

746 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

12 Experts available now in Live!

Get 1:1 Help Now