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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

932 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

13 Experts available now in Live!

Get 1:1 Help Now