?
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
Medium Priority
?
459 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 2000 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

762 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