Solved

Preventing identical records

Posted on 2006-11-01
6
220 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 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 …

777 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