Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 11th, 2008, 01:30 AM
Registered User
 
Join Date: Sep 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to asad_black
Default how to check the username already exists in table

hi every one...!

em creating a create user account page.

i want to check the username is already exists in my database table or not?

if exists then give me a error message that username is already exists if not then account will be successfully create.

em using this code but its not work correctly..

i dont know whatz de problem. and please remember em using Access database.

please point out my mistakes.





<script language="VB" runat="server">
Dim objConnection As OleDbConnection
Sub Page_Load(Source As Object, E As EventArgs)
objConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " _
& "DATA SOURCE=" _
& Server.MapPath("nwind.mdb;"))
End Sub




Sub btnInsert_Click(ByVal Sender As Object, ByVal E As EventArgs)

Dim query As String
query = "Select Count(*) From login Where username = ? "
Dim result As Integer = 0

If result > 0 Then



Dim strSQL As String = "INSERT INTO login (username, [password]) " & _
"VALUES (?, ?) "


Dim dbComm As New OleDbCommand(strSQL, objConnection)
dbComm.Parameters.Add("UserName", OleDbType.VarChar, 32, "UserName")
dbComm.Parameters.Add("Password", OleDbType.VarChar, 128, "Password")

dbComm.Parameters("UserName").Value = txtUserName.Text
dbComm.Parameters("Password").Value = txtPassword.Text

Try
objConnection.Open()
dbComm.ExecuteNonQuery()
Catch ex As Exception
Response.Write(ex.Message)
Response.End()
Finally
If objConnection.State = ConnectionState.Open Then
objConnection.Close()
End If
End Try

Response.Write("A new record has been added")
Response.End()


Else
Response.Write("your username is already exist")
End If


End Sub

</script>

 
Old September 12th, 2008, 08:49 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 207
Thanks: 2
Thanked 15 Times in 15 Posts
Default

I think your issue is right here:

If result > 0 Then

it should be

If result = 0 Then

because if i'm reading your code correctly it looks as if you are saying... if there are more than zero entries in the database that have this username then add another otherwise if there are zero tell the user that the name is already in use.

Jason Hall
 
Old September 15th, 2008, 07:19 AM
Authorized User
 
Join Date: Mar 2007
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

As already stated, the obvious error is that you're only inserting the new user if it already exists.

I'd add a couple of efficiency suggestions too though, if you don't mind. For one, you've declared a variable called 'query' to hold the initial query, then declared another string var named strSQL to hold the subsequent insert statement. I'd reuse the one string var personally. And the 'Select Count(*)...' can be more efficiently coded as 'Select count(1)...'. As well as that, your messages need to be clearer - the end user would probably prefer to know that his / her user-name has been successfully added, not that a new record has been added, and fwiw, the syntax of the error is a bit mangled - try 'Your username already exists.'

I'm assuming that you've omitted some code from your question though, as there's nothing that allocates a value to 'result' from the result of the query, nor to populate the parameter value in the initial query. Presumably that was left out just for this post though.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie question: check if table exists in DB savoym Java Databases 4 March 9th, 2012 06:57 AM
check whether a particular session exists or not chayanvinayak PHP How-To 1 May 1st, 2006 04:09 PM
check if a column exists in the table sands SQL Server 2000 2 April 21st, 2006 11:08 PM
check first to see if the file exists crmpicco Classic ASP Professional 2 December 1st, 2005 12:34 PM
how to check that node is exists debuajm General .NET 0 June 8th, 2004 02:07 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.