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:
Dim Obj As New UserAccount
Obj.PassedText = UNameTB.Text
This is the
UserAccount Form:
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
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
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..