PDA

View Full Version : question


pgsaravanan
07-03-2003, 12:29 PM
how to check the already exist username

raf
07-03-2003, 01:07 PM
I suppose you mean 'checking if that username is already in the database' ?
You just run a select with a count on the records with that username.

sql="SELECT Count(*) as totalcount FROM table WHERE usernamevariable='theusernam'"
rsUsers.Open sql, conDB

if rsUsers.Fields("totalcount") > 0 then
response.write("This username is taken.")
else
your code
end if

rsUsers.Close
set rsUsers = Nothing

photoman
07-03-2003, 04:17 PM
or another method is this

on registration page

Username: <Input type = "text" name = "usernamereg">

On reg validation page
[code]
Dim Conn
Dim RS
Set conn = Server.CreateObject("ADODB.Connection")
conn.open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("dbname")
Set RS = Server.CreateObject("ADODB.Recordset")
RS.open "Select * From Users Where Users.Username ='" & Request.form("usernamereg") & "'", conn, 1, 1
If RS.RecordCount > 0 Then
Response.write("The Username " & Request.form("Usernamereg") & "is not avaliable Please try again")
Else
Continue REgistration
End If

arnyinc
07-03-2003, 05:27 PM
Originally posted by photoman
or another method is this


The select * will put a heavier load on the server than count(*) and the rs.recordcount presents the possibility of coming up with this problem
http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=139

oracleguy
07-03-2003, 07:02 PM
Originally posted by photoman
or another method is this

on registration page

Username: <Input type = "text" name = "usernamereg">

On reg validation page
[code]
Dim Conn
Dim RS
Set conn = Server.CreateObject("ADODB.Connection")
conn.open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("dbname")
Set RS = Server.CreateObject("ADODB.Recordset")
RS.open "Select username From Users Where Username ='" & Request.form("usernamereg") & "'", conn, 1, 1
If RS.EOF Then
Response.write("The Username " & Request.form("Usernamereg") & "is not avaliable Please try again")
Else
Continue REgistration
End If

With those changes it would be more compatible and less stressful on the server than what you had before. Although I think using the count() function still might be faster.

raf
07-03-2003, 07:40 PM
count() is faster and generates less trafic (recordset only contains only one valu) + less load on the webserver + quite universal.

But sometimes select username from users etc is better.

My policy is :
- if you only want to check if the username already exstis, use count().
- if you wan't to display info about that already existing username (like registrationdate or so), then use select username, regdate etc (but this is quite rare)

photoman
07-03-2003, 08:08 PM
Originally posted by arnyinc
The select * will put a heavier load on the server than count(*) and the rs.recordcount presents the possibility of coming up with this problem
http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=139

That problem is the reason I have conn,1 ,1 It takes care of that porblem

scroots
07-03-2003, 10:22 PM
can you not just set your database field to only accept only a username once, so it is Unique? I thought there is a way in access.


scroots

raf
07-03-2003, 11:12 PM
can you not just set your database field to only accept only a username once, so it is Unique? I thought there is a way in access.

Indeed. You can do that (in any db) by creating an index on the username variabel and choose 'no duplicate'. And you should do that at db -level (to speed up the query and prevent duplicate entrys --> even with the select-check as described above, there is a very limmited chance that another user inserts that username between your select and your insert)

But ... what message will the db-return? By running the select, you know what response to expect and check for so it is easier to give the client some usefull feedback, and maybe redirect him to the loginpage or supply a 'forgotten pasword' link or so.

oracleguy
07-03-2003, 11:51 PM
Originally posted by scroots
can you not just set your database field to only accept only a username once, so it is Unique? I thought there is a way in access.


scroots

If you do this, and you should, but don't have some kind of error catching, the server is just going to output a HTTP 500 error and the user wouldn't know what the problem was.

scroots
07-05-2003, 10:05 AM
didn't think about that one

scroots

whammy
07-08-2003, 01:20 AM
Since the original poster hasn't responded, I'm gonna give this a day or so and then close the thread.

Also, pgsaravanan, please read the forum rules, we need a lot more information to be able to help you efficiently.