Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi im having trouble making this work again. Before it is working but then when i protected my database with a password it stopped working.

in my login form, if a username and password match, it will pass the usernamme by this code:
VB
'passed details starts here
                Dim Obj As New UserAccount
                Obj.PassedText = UNameTB.Text
                'end


This is the UserAccount Form:
VB
Public Class UserAccount
    Private mstrConn As String = _
        "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source=" & Application.StartupPath & "\InfoDB.accdb;" & _
        "Persist Security Info=False;" & _
        "Jet OLEDB:Database Password=adminGMRT;"

    Private _CurrentUser As UserData
    Private _MyDB As String = Application.StartupPath & "\InfoDB.accdb"";Jet OLEDB:Database Password=adminGMRT;"
    Private _Repository As MyRepository = New MyRepository(_MyDB)
    Private PositionDB As String

    Public Property [PassedText]() As String
        Get
            Return IIf(_CurrentUser Is Nothing, "", _CurrentUser.UserName)
        End Get
        Set(ByVal Value As String)
            _CurrentUser = _Repository.GetUser(Value)
            If _CurrentUser IsNot Nothing Then RegisterLbl.Text = _CurrentUser.FirstName + " " + _CurrentUser.LastName + "!"
            PositionDB = _CurrentUser.Position
        End Set
    End Property


    Private Sub UserAccount_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class


The Repository class
VB
Imports System.Data.OleDb
Public Class MyRepository
    Private _connection As OleDbConnection
    Private _dbPath As String

    Private Function AccessConnectionString(ByVal DatabasePath As String)
        Return New OleDbConnectionStringBuilder() With {
            .Provider = "Microsoft.ACE.OLEDB.12.0",
            .DataSource = DatabasePath,
        .PersistSecurityInfo = False     
                }.ConnectionString
    End Function

    Public Sub New(ByVal DatabasePath As String)
        _dbPath = DatabasePath
        _connection = New OleDbConnection(AccessConnectionString(DatabasePath))
    End Sub

    Public Function GetUser(ByVal UserName As String) As UserData
        Dim dt As DataTable = FillTable(
            "SELECT * FROM DBtable WHERE UserName = @UserName",
            {New OleDbParameter("@UserName", UserName)}
        )
        If dt.Rows.Count = 0 Then
            Return Nothing
        Else
            Return UserFromRow(dt.Rows(0))
        End If
    End Function

    Public Function GetUsers() As IEnumerable(Of UserData)
        Return From row In FillTable("SELECT * FROM DBtable")
               Select UserFromRow(row)
    End Function

    Private Function FillTable(ByVal CommandText As String, Optional ByVal Parms() As OleDbParameter = Nothing) As DataTable
        Dim dt As New DataTable
        Using adp = New OleDbDataAdapter(CommandText, _connection)
            If Parms IsNot Nothing Then
                adp.SelectCommand.Parameters.AddRange(Parms)
            End If
            adp.Fill(dt)
        End Using
        Return dt
    End Function

    Private Function UserFromRow(ByVal row As DataRow) As UserData
        Return UserData.CreateUser(
            DBValue(Of String)(row("UserName")),
            DBValue(Of String)(row("PassCode")),
            DBValue(Of String)(row("FirstName")),
            DBValue(Of String)(row("LastName")),
        DBValue(Of String)(row("Position"))
        )
    End Function

    Private Function DBValue(Of T)(ByVal value As Object) As T
        If value Is DBNull.Value Then
            Return CType(Nothing, T)
        Else
            Return CType(value, T)
        End If
    End Function
End Class


the UserData class
VB
Public Class UserData
        Public Property UserName As String
        Public Property Password As Security.SecureString
        Public Property FirstName As String
        Public Property LastName As String
        Public Property Position As String

        Private Sub New()
        End Sub

    Public Shared Function CreateUser(ByVal Username As String, ByVal Password As String, ByVal FirstName As String, ByVal LastName As String, ByVal Position As String) As UserData
        Return New UserData() With {
                .UserName = Username,
                .Password = ConvertToSecure(Password),
                .FirstName = FirstName,
                .LastName = LastName,
                .Position = Position
                    }
    End Function

        Private Shared Function ConvertToSecure(ByVal value As String) As Security.SecureString
            Dim s As New Security.SecureString
            If value IsNot Nothing Then
                For Each c As Char In value
                    s.AppendChar(c)
                Next
            End If
            Return s
        End Function
    End Class


The login sn working. When i run this, the login shows "Successful". The User Account Form is Showing.

But then there is a label in UserAccount that must be change to the Name of the logged-in user. As i have said above, it is showing before i put a password in my database. So i guess the mistake will be in the coding of database connection string in UserData and Repository. I have been debugging this for hours but i cant find it.. The code is not showing any error, but it does not output the name of the user either.... Help me please, im getting lost with all these codes..
Posted

1 solution

Private _MyDB As String = Application.StartupPath & "\InfoDB.accdb"";Jet OLEDB:Database Password=adminGMRT;"


This line here throws an unnecessary " into your connection string...
 
Share this answer
 
Comments
kimsnap 29-Mar-12 2:09am    
Hi can you please explain? i did not get what you mean..

Private _MyDB As String = Application.StartupPath & "\InfoDB.accdb" that is the original line,
Jet OLEDB:Database Password=adminGMRT; this is what i append for the database's password, Is my coding wrong?
_Damian S_ 29-Mar-12 2:14am    
This bit here: "\InfoDB.accdb"";Jet OLEDB:Database Password=adminGMRT;" will result in the following string: \InfoDB.accdb";Jet OLEDB:Database Password=adminGMRT;

See the extra " after accdb and before ;?

You want to use this: "\InfoDB.accdb;Jet OLEDB:Database Password=adminGMRT;"

kimsnap 29-Mar-12 2:29am    
i tried changing that bit. but the username is still not passing to the useraccount form,, that bit about username..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900