Solved

SQL Datareader only checks first line in database

Posted on 2007-11-15
7
415 Views
Last Modified: 2008-02-01
Hi

I am building a .net website and want to be able to use a login control to access a page where the username and password are taken from an existing database.

The code I have used is below:


Now this works great.... as long as you are the first person in the database table. For some reason the datareader doesn't read all the rows to match the username and password entered.

Any help would be greatly appreciated.
Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As AuthenticateEventArgs) 

    Dim Authenticated As Boolean = False 

    Authenticated = SiteLevelCustomAuthenticationMethod(Login1.UserName, Login1.Password) 

    e.Authenticated = Authenticated 

    If Authenticated = True Then 

        Response.Redirect("Home.aspx") 

    End If 

End Sub 
 

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

    Dim boolReturnValue As Boolean = False 

    ' Insert code that implements a site-specific custom 

    ' authentication method here. 

    ' This example implementation always returns false. 

    Dim strConnection As String = "server=dtpxp-skumari;database=master;uid=sa;pwd=;" 

    Dim Connection As New SqlConnection(strConnection) 

    Dim strSQL As String = "Select * From Employee" 

    Dim command As New SqlCommand(strSQL, Connection) 

    Dim Dr As SqlDataReader 

    Connection.Open() 

    Dr = command.ExecuteReader() 

    While Dr.Read() 

        If (UserName = Dr("name").ToString()) And (Password = Dr("Password").ToString()) Then 

            boolReturnValue = True 

        End If 

        Dr.Close() 

        Return boolReturnValue 

    End While 

End Function

Open in new window

0
Comment
Question by:DJFuller
7 Comments
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 83 total points
ID: 20288236
you close the datareader and return inside the while loop... bad move.
While Dr.Read()         

  If (UserName = Dr("name").ToString()) And (Password = Dr("Password").ToString()) Then        

     boolReturnValue = True       

  End If        

End While 

Dr.Close()         

Return boolReturnValue     

Open in new window

0
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 83 total points
ID: 20288239
Dr = command.ExecuteReader()    
While Dr.Read()        
   If (UserName = Dr("name").ToString()) And (Password = Dr("Password").ToString()) Then
               boolReturnValue = True        
   End If
end while
Dr.Close()        
Return boolReturnValue
0
 
LVL 25

Accepted Solution

by:
imitchie earned 84 total points
ID: 20288347
if i may, break to speed things up

While Dr.Read()        
  If (UserName = Dr("name").ToString()) And (Password = Dr("Password").ToString()) Then        
     boolReturnValue = True
     break
  End If        
End While
Dr.Close()        
Return boolReturnValue    
0
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.

 
LVL 25

Expert Comment

by:imitchie
ID: 20288382
replace

    Dim strSQL As String = "Select * From Employee"
    Dim command As New SqlCommand(strSQL, Connection)
    Dim Dr As SqlDataReader
    Connection.Open()
    Dr = command.ExecuteReader()
    While Dr.Read()
        If (UserName = Dr("name").ToString()) And (Password = Dr("Password").ToString()) Then
            boolReturnValue = True
        End If
        Dr.Close()
        Return boolReturnValue
    End While

with

    Dim strSQL As String = "Select [name] From Employee where [name] = '" + Replace(UserName, "'", "''") + "' and [Password]= '" + Replace(Password, "'", "''") + "'"
    Dim command As New SqlCommand(strSQL, Connection)
    Dim Dr As SqlDataReader
    Connection.Open()
    Dr = command.ExecuteReader()
    boolReturnValue = Dr.Eof
    Dr.Close()
    Return boolReturnValue
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20288384
grr... time tosleep
    Dim strSQL As String = "Select [name] From Employee where [name] = '" + Replace(UserName, "'", "''") + "' and [Password]= '" + Replace(Password, "'", "''") + "'"

    Dim command As New SqlCommand(strSQL, Connection)

    Dim Dr As SqlDataReader

    Connection.Open()

    Dr = command.ExecuteReader()

    boolReturnValue = not Dr.Eof

    Dr.Close()

    Return boolReturnValue 

Open in new window

0
 
LVL 2

Author Comment

by:DJFuller
ID: 20288665
Oh my god.. what an idiot I am..

Thanks all of you... I guess I can share points out.

imitchie: what does adding break do?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20290034
>imitchie: what does adding break do?
it finishes the while immediately. as at that place, you knew the username/password is correct, no need to loop further in the DataReader.

the suggestion of imitchie to include the username/password test directly in the query is obviously the most efficient method, but could even be made better by using sqlcommand and sqlparameter objects ... but that is another discussion. just read up on those key terms will help you coding alot.
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

Suggested Solutions

Introduction (All good things must come to an end (http://en.wikipedia.org/wiki/All_Good_Things...))The original MySQL API (http://php.net/manual/en/book.mysql.php) has gone away, deprecated by PHP in Version 5.5, and removed from PHP in all current…
New Relic: Our company recently started researching several products to figure out what were the best ways for us to increase our web page speed and to quickly identify performance problems that we may be having. One of the products we evaluated wa…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

867 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