ASP.NET

Moderators: None (Apply to moderate this forum)
Number of threads: 1727
Number of posts: 3292

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Comparing value in textbox with a field in a database? Posted by stokefan on 21 Dec 2004 at 6:00 AM
Hi all,

I'm new to ASP.net forms authentication, so I'm making a simple login feature to my site, where the user enters their username, password and first name into my database. I can get this to insert correctly.

On the login screen though, when the user enters their username and password, how do I compare what they've submitted with what's stored in the database?

Many thanks,

stokefan
Report
Re: Comparing value in textbox with a field in a database? Posted by iwilld0it on 21 Dec 2004 at 6:46 AM
Usually when creating a login, I build a sql query to lookup a user based on the username and password:

Dim Sql As String = "SELECT UserID FROM LoginTable " & _
                    "WHERE UserName = '" & txtUserName.Text & "' "
                    "AND Password = '" & txtPassword.Text & "'"

Dim con As New SqlConnection(connectString)
Dim cmd As New SqlCommand(Sql, con)
Dim reader As New SqlDataReader

con.Open
reader = cmd.ExecuteReader

If Not reader.Read Then
    ' Login was unsuccessful
    ' Show error message in a label
    lblMessage.Text = "Your username or password was incorrect!"
End If
reader.Close
con.Close


: Hi all,
:
: I'm new to ASP.net forms authentication, so I'm making a simple login feature to my site, where the user enters their username, password and first name into my database. I can get this to insert correctly.
:
: On the login screen though, when the user enters their username and password, how do I compare what they've submitted with what's stored in the database?
:
: Many thanks,
:
: stokefan
:

Report
Re: Comparing value in textbox with a field in a database? Posted by stokefan on 21 Dec 2004 at 6:54 AM
This message was edited by stokefan at 2004-12-21 7:8:34

hi, many thanks for your response there.

I wonder if you could explain to me please what the code does? i.e. the SQL statement's '" bits I dont really understand.

Also, would there be any chance I could get the query in Access form instead of sql please?

Many, many thanks.

Much appreciated.

stokefan.

: Usually when creating a login, I build a sql query to lookup a user based on the username and password:
:
:
: Dim Sql As String = "SELECT UserID FROM LoginTable " & _
:                     "WHERE UserName = '" & txtUserName.Text & "' "
:                     "AND Password = '" & txtPassword.Text & "'"
: 
: Dim con As New SqlConnection(connectString)
: Dim cmd As New SqlCommand(Sql, con)
: Dim reader As New SqlDataReader
: 
: con.Open
: reader = cmd.ExecuteReader
: 
: If Not reader.Read Then
:     ' Login was unsuccessful
:     ' Show error message in a label
:     lblMessage.Text = "Your username or password was incorrect!"
: End If
: reader.Close
: con.Close
: 

:
: : Hi all,
: :
: : I'm new to ASP.net forms authentication, so I'm making a simple login feature to my site, where the user enters their username, password and first name into my database. I can get this to insert correctly.
: :
: : On the login screen though, when the user enters their username and password, how do I compare what they've submitted with what's stored in the database?
: :
: : Many thanks,
: :
: : stokefan
: :
:
:


Report
Re: Comparing value in textbox with a field in a database? Posted by iwilld0it on 21 Dec 2004 at 9:51 AM
Access form ...

Dim Sql As String = "SELECT UserID FROM LoginTable " & _
                    "WHERE UserName = '" & txtUserName.Text & "' "
                    "AND Password = '" & txtPassword.Text & "'"

Dim con As New OleDBConnection(connectString)
Dim cmd As New OleDBCommand(Sql, con)
Dim reader As New OleDBDataReader

con.Open
reader = cmd.ExecuteReader

If Not reader.Read Then
    ' Login was unsuccessful
    ' Show error message in a label
    lblMessage.Text = "Your username or password was incorrect!"
End If
reader.Close
con.Close


The SQL statement is already in Access form, since SQL is nearly universal enough. However, the SQL query ...

"SELECT UserID FROM LoginTable " & _
"WHERE UserName = '" & txtUserName.Text & "' "
"AND Password = '" & txtPassword.Text & "'"


... reads from an example database table called "LoginTable", which has at least 3 columns:

UserID (AutoNumber)
UserName (Text)
Password (Password)

The SQL statement says to return A record where UserName and Password is equal to something. In our case we are dynamically building the SQL query, based on the values of two TextBox ASP.NET controls called "txtUserName" and "txtPassword". So if someonem on the login form enters "jdoe" in the txtUserName textbox and someone enters "test" in the txtPassword textbox, the SQL query would look like this in the long run:

"SELECT UserID FROM LoginTable " & _
"WHERE UserName = 'jdoe' "
"AND Password = 'test'"


This next part executes the sql query and returns an OleDBDataReader object:

reader = cmd.ExecuteReader


Basically, this returns the results of the SQL query. If there are any results, then the data readers Read() function will return the boolean value of "True". In our code, if the Read() method returns false, then the login information was incorrect.

If Not reader.Read Then
    ' Login was unsuccessful
    ' Show error message in a label
    lblMessage.Text = "Your username or password was incorrect!"
End If


NOTE: In the code I use OleDB objects from the System.Data.OleDb namespace, because that is the only way to work with Access database. In the code you will notice that I used the variable "connectString" in the OleDbConnection objects constructor. I assumed that variable was set elsewhere in code. However, you would probably set that variable priorly in code like so:

Dim connectString As String

connectString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                "Data Source=C:\myPath\accessFile.mdb;" & _
                "User ID=" & _
                "Password=" 



If you need to know more about the different connection strings, go to:

http://www.able-consulting.com/ADO_Conn.htm

ADO.NET database programming is an extensive subject, which I suggest picking up a good book on.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.