Solved

Preventing identical records

Posted on 2006-11-01
6
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

733 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