Solved

Login Control UserName Only Accept the Initial Test Value of UserName and Password, Can't Test Any other set

Posted on 2012-12-28
2
456 Views
Last Modified: 2012-12-30
Hi,

I am a beginner of .Net Web Developer.

I programmed a simple Web app in VB.NET 2005 to let an user log in at first, and the UserName will be passed to the next page for displaying his or her relevant information .

the Login Control connects to a data table in SQL Server 2005 for log in authentication. I used UserID stored in the database as value of UserName text field of the Login control.

The problem is, during my many tests, only the very first UserID and password (the password is actually an user's last name) got successfully log in, no matter how many time to repeat to use this set of value. Except this set of value, the Login process was never succeed when using any other pairs of UserID and LastName, even I ensured to type in correct value in the text fields.

Your advise will be very appreciate!

Part of my code: -----------------------------------------------------------------

Private Function YourValidationFunction(ByVal UserName As String, ByVal Password As String) As Boolean


        Dim boolReturnValue As Boolean = False
        Dim strConnection As String = "Data Source=myServerName;Initial Catalog=myDatabaseName;Integrated Security=True;"
        Dim sqlconnection As New SqlClient.SqlConnection(strConnection)
        Dim SQLQuery As String = "SELECT distinct [UserId],[Last name] FROM myUserTable"
        Dim command As New SqlClient.SqlCommand(SQLQuery, sqlconnection)
        Dim Dr As SqlClient.SqlDataReader
        sqlConnection.Open()
        Dr = command.ExecuteReader()
        While Dr.Read()
            If (UserName = Dr("UserID").ToString()) And (Password = Dr("Last name").ToString()) Then
                boolReturnValue = True
            End If
            Dr.Close()
            Return boolReturnValue
        End While
        Return boolReturnValue
    End Function
0
Comment
Question by:GaiaGia
[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
2 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 38728215
The problem is that you have return statement within your While loop which means that while loop only executes once.

I would change your code to below

Private Function YourValidationFunction(ByVal UserName As String, ByVal Password As String) As Boolean
        Dim boolReturnValue As Boolean = False
        Dim strConnection As String = "Data Source=myServerName;Initial Catalog=myDatabaseName;Integrated Security=True;"
        Dim sqlconnection As New SqlClient.SqlConnection(strConnection)
        Dim SQLQuery As String = "SELECT Count(UserID) FROM MyUserTable Where [UserId]=@UserID AND [Last name]=@LastName"
        Dim command As New SqlClient.SqlCommand(SQLQuery, sqlconnection)
        sqlConnection.Open()
        command.Parameters.AddWithValue("@UserID", UserName)
        command.Parameters.AddWithValue("@LastName", Password)
        Dim cnt As Integer = command.ExecuteScalar()
        If cnt = 1 Then
            boolReturnValue = True
        End If
        command.Dispose
        sqlConnection.Dispose
        Return boolReturnValue
    End Function

Open in new window

0
 

Author Closing Comment

by:GaiaGia
ID: 38730984
Thank you, CodeCruiser!
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

732 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