Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 136,549 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,861 people online right now. Registration is fast and FREE... Join Now!




Login help!

 
Reply to this topicStart new topic

Login help!, Index Out of Range Exeption!!

revolution14
22 Aug, 2008 - 06:03 AM
Post #1

New D.I.C Head
*

Joined: 11 Dec, 2007
Posts: 41


My Contributions
hello there, here is the code i am using

CODE

Imports System.Data.OleDb
Module Module1

    Public admin As Boolean  'Use to identify if current user is Administrator

End Module

Public Class StaffLogin

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        username.Clear()
        password.Clear()

    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\Users\In The Know\Documents\Database1.mdb")
        Dim cmd As OleDbCommand = New OleDbCommand("SELECT Username FROM StaffLogin WHERE Username = '" & username.Text & "' AND Password = '" & password.Text & "' ", cn)

        cn.Open()
        Dim dr As OleDbDataReader = cmd.ExecuteReader()
        If (dr.Read() = True) Then
            admin = dr("isManager")
            If (admin = True) Then
                MsgBox("Login Successful, " & username.Text & "!")
            Else
                MsgBox("you are not manager!")
            End If
        Else
            MsgBox("invalid login")
        End If
        cn.Close()

    End Sub
End Class



however i get the error message saying

blink.gif admin = dr("isManager") - index is out of range!! blink.gif

any ideas??
User is offlineProfile CardPM
+Quote Post

modi123_1
RE: Login Help!
22 Aug, 2008 - 07:45 AM
Post #2

D.I.C Addict
Group Icon

Joined: 12 Jun, 2008
Posts: 531



Thanked: 13 times
Dream Kudos: 100
My Contributions
Yeah.. there's a few problems that center on the "admin = dr("isManager")".

A datareader is an array of information coming into the object from your select statement. As an array you should be interacting with it like dr(0)... dr(1).. etc.. In your case you only want the dr(0) because you only have one set of data coming back from the select statement. That's why you get the 'out of range' error - VB is not sure how to interpret 'isManager' as an index.

Now, what you should be doing is taking your data reader and converting it into a .NET dataset or table. In your case your table would have only one column - a string called userName.

Copying out of the MSDN 'populating datasets from a data adapter':

CODE

' Assumes that connection is a valid SqlConnection object.
Dim queryString As String = _
  "SELECT CustomerID, CompanyName FROM dbo.Customers"
Dim adapter As SqlDataAdapter = New SqlDataAdapter( _
  queryString, connection)

Dim customers As DataSet = New DataSet
adapter.Fill(customers, "Customers")


After you get your table populated then you can check to see if there are rows in the table and by the looks of it if there are rows then they are a manager.. capice?
User is offlineProfile CardPM
+Quote Post

revolution14
RE: Login Help!
22 Aug, 2008 - 08:35 AM
Post #3

New D.I.C Head
*

Joined: 11 Dec, 2007
Posts: 41


My Contributions
hmmm i still dont understand.

in my database i have four colums

id, username, password and isManager
User is offlineProfile CardPM
+Quote Post

modi123_1
RE: Login Help!
22 Aug, 2008 - 08:40 AM
Post #4

D.I.C Addict
Group Icon

Joined: 12 Jun, 2008
Posts: 531



Thanked: 13 times
Dream Kudos: 100
My Contributions
What don't you understand? why you would only have a datatable with one column? Your select statement only RETURNS one column!

QUOTE
Dim cmd As OleDbCommand = New OleDbCommand("
SELECT Username
FROM StaffLogin
WHERE Username = '" & username.Text & "' AND Password = '" & password.Text & "' ", cn)


Now if you are only returning the boolean (or what i assume to be a boolean) 'isManager' you would need to change your select statement to
QUOTE
Dim cmd As OleDbCommand = New OleDbCommand("SELECT isManager FROM StaffLogin WHERE Username = '" & username.Text & "' AND Password = '" & password.Text & "' ", cn

As it is currently (your stuff) you are not RETURNING that column.


If you are confused about the indexing of the datareader.. well.. just say you are and we can go down that path.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 11:07PM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month