Results 1 to 9 of 9

Thread: [RESOLVED] Help with login form

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Posts
    9

    Resolved [RESOLVED] Help with login form

    Ok so basically I have a login form connected to a database and it (Database) contains my usernames and passwords. I also included a security level so that if a certain member logs on, features to them will be disabled. So For example if Katie logged in she can view all records BUT cannot delete them.

    However there is a problem, each time I try to log in the following message appears:

    "object reference not set to an instance of an object" either that message appears or this one:

    "not allowed to change the 'connectionstring' property. the connection's current state is open."

    I've been trying to solve this problem for a while now and I can't fix it.

    Below is the code for my login form:



    Code:
    Imports System.Data.OleDb
    
    
    Public Class frmLogin
    
        Dim con As New OleDb.OleDbConnection
        Dim dbProvider As String
        Dim dbSource As String
        Dim ds As New DataSet
        Dim da As OleDbDataAdapter
        Dim sql As String
        Dim dt As New DataTable
        Dim DReader As OleDbDataReader
    
    
         Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
    
    
     'Dim dr As DataRow
            Dim dr As OleDbDataReader
            'Dim dt As DataTable
            Dim user As String = Trim(txtUsername.Text)
            Dim pword As String = Trim(txtPassword.Text)
    
            ' If the username or password fields are empty then this displays an error message:
            If user = "" Then
                MessageBox.Show("Please enter the username.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
            ElseIf pword = "" Then
                MessageBox.Show("Please enter the password.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
    
            If user <> "" And pword <> "" Then
    
            End If
    
            dbSource = "Data Source =G:\Assignment 2 codes\ValleyMilk.accdb"
    
            dbProvider = "PROVIDER = Microsoft.Ace.OLEDB.12.0;"
    
            Try
                con.ConnectionString = dbProvider & dbSource
                con.Open()
                sql = "SELECT * FROM Users WHERE Username= user AND [Password]= pword"
                Dim cmd As OleDbCommand = New OleDbCommand(sql, con)
    
                cmd.Parameters.AddWithValue("Username", user)
                cmd.Parameters.AddWithValue("Password", pword)
    
                dr = cmd.ExecuteReader
                ' If dr.Read() = True And user = dr("Username").ToString And pword = dr("Password").ToString Then
         'LogginUserLevel and LoggedinUser are from Module1.vb
    
                LoggedinUserLevel = DReader("SecurityLevel")
                LoggedinUser = DReader("Username")
    
                If dr.Read() = True Then
                    MsgBox("Welcome back !")
                    Me.Hide()
                    frmMainMenu.Show()
                Else
                    MsgBox("Login Failure")
    
                    dr.Close()
                    con.Close()
    
                End If
    
            Catch errObj As Exception
    
                MessageBox.Show(errObj.Message)
            End Try


    Any help on this would be great.


  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Help with login form

    on what lines of code do these errors occur?
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Posts
    9

    Re: Help with login form

    I suspect its this part:

    Code:
          LoggedinUserLevel = DReader("SecurityLevel")
                LoggedinUser = DReader("Username")
    
                If dr.Read() = True Then
                    MsgBox("Welcome back !")
                    Me.Hide()
                    frmMainMenu.Show()
                Else
                    MsgBox("Login Failure")
    
                    dr.Close()
                    con.Close()

    I removed the "Dreader" part and the login worked fine, but I need the securitylevel part to be in. Also the following variables:

    LoggedinUserLevel = DReader("SecurityLevel")
    LoggedinUser = DReader("Username")

    Are in a module so they will be recognised everywhere.


    If it helps I can send you the form and the database if you want.

  4. #4
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Help with login form

    I'mm not well versed in oledb stuff, but when an object is not set to a reference, it means that even you may have created the variable, you never instance it.

    Are you mistakenly using dr as the reader when you should be using DReader?
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Posts
    9

    Re: Help with login form

    That's what I thought it was but the same messages still appear.

  6. #6
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Help with login form

    it doesn't look like you are ever creating the Dreader object. You do this...

    dr = cmd.ExecuteReader

    and then are trying to use Dreader to get the value which are in dr. (Unless you are using Dreader elsewhere)
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  7. #7
    Lively Member
    Join Date
    Jul 2011
    Posts
    65

    Re: Help with login form

    Just a suggestion use try catch finally. You appear to only close connection if it was sucessful. Should close it no matter what happens to prevent memory leaks and other problems.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Help with login form

    Quote Originally Posted by Lewis194 View Post
    I suspect its this part:

    Code:
          LoggedinUserLevel = DReader("SecurityLevel")
                LoggedinUser = DReader("Username")
    
                If dr.Read() = True Then
                    MsgBox("Welcome back !")
                    Me.Hide()
                    frmMainMenu.Show()
                Else
                    MsgBox("Login Failure")
    
                    dr.Close()
                    con.Close()

    I removed the "Dreader" part and the login worked fine, but I need the securitylevel part to be in. Also the following variables:

    LoggedinUserLevel = DReader("SecurityLevel")
    LoggedinUser = DReader("Username")

    Are in a module so they will be recognised everywhere.


    If it helps I can send you the form and the database if you want.
    You don't have to suspect anything. The IDE will tell you exactly where the error occurred. You're catching the exception and displaying the message but the exception contains much more information than that. Look at it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Posts
    9

    Re: Help with login form

    Hi people sorry for the late response I've been a bit busy but I solved the problem, turns out my 'Dreader' was in the wrong place it should have been within the If statement below it. Anyways thank you all for replying to this thread.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width