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
450 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
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now