hello folks,
let me tell you the plot first.
I am developing a windows application through vb.net,i have done the database connectivity through ms access, which has only two columns, i.e firstname and lastname.
what i want is to maintain a session id so when i browse through all the forms the application knows both the names of the user.

please suggest me the ways to do this as i am new to vb.net.
thanks in advance.
regards
aditya

Dani AI

Generated

As described, you want the user to sign in once and have their name available while they move through several WinForms. ' idea (a login dialog plus a central place to hold the values) is the right starting point. For a slightly more maintainable pattern, keep the login UI but store the user in a small session object rather than scattering public variables across the project.

A minimal, reusable session class:

Public Class UserSession
    Private Shared _current As UserSession

    Public Shared ReadOnly Property Current As UserSession
        Get
            If _current Is Nothing Then _current = New UserSession()
            Return _current
        End Get
    End Property

    Public Property UserId As Nullable(Of Integer)
    Public Property FirstName As String
    Public Property LastName As String

    Public ReadOnly Property FullName As String
        Get
            Return (FirstName & " " & LastName).Trim()
        End Get
    End Property

    Public Sub Start(id As Nullable(Of Integer), first As String, last As String)
        UserId = id
        FirstName = first
        LastName = last
    End Sub

    Public Sub EndSession()
        UserId = Nothing
        FirstName = Nothing
        LastName = Nothing
        _current = Nothing
    End Sub
End Class

Use it after validating the login (set UserSession.Current.Start(...)). Other forms can read UserSession.Current.FullName or keep a private reference to UserSession.Current in their constructor or Load event — that keeps code explicit and testable compared with widely-scoped globals.

Practical tips: store and use the database primary key (UserId) when saving marks instead of names, and always use parameterized queries with OleDb to avoid data errors. If the login must run before any other form appears, show it modally from Sub Main (or before Application.Run) so other forms never see an uninitialized session. Clear the session on logout and avoid persisting sensitive info in plain text; if you want "remember me" behavior use My.Settings or a secure store, not raw globals.

Recommended Answers

All 3 Replies

A simple way is to Use a log in form to ask for them ( a 2 textbox form and an accept and cancel buttons may be enough)
If the user cancels, then close the application.

You can verify if this is an allowed user by searching it in the database of allowed users.

If the user is allowed, then register the start of the session in a public variable in the main module of your application, to be used across all your forms.

Hope this helps

hey lolafuertes,
many thanks for replying.
actually as i have mentioned earlier that i am new to vb.net,can you please present your suggestion with some coding examples.
and also in this application the user is asked once to enter his name and then his marks are fed into the database after the redirection of 5 forms.

Assume you already have a main form in the project called frmMain that is the current startup object.
Add a new form to your project called frmLogin
In this form add a label "First name" and a text box textBoxFirstName.
Then add a label "Last name" and a text box textBoxLastName.
Add a button btnAccept and a button btnCancel
Define the btnAccept as the default button of the form (in the properties of the form)
Also define the btnCancel as the canccel button of the form
Then, on the properties of the buttons click on the event Click to capture it.

On the btnAccept_Click event add

If textBoxFirstName.Text.Trim.Length = 0 or textBoxLastName.Text.Trim.Length = 0 then
    MsgBox "Fill both"
    Exit Sub
End If
CurrentUserFirstName = textBoxFirstName.Text.Trim
CurrentUserLastName = textBoxLastName.Text.Trim
DialogResult=System.Windows.Forms.DialogResult.Yes
Hide

On the btnCancel_Click event add

DialogResult=Systtem.Windows.Forms.DialogResult.No
Hide

Just Create a New Module in your project
In this module add the following:

Public CurrentUserFirstName as string
Public CurrentUserLastName as String

Then add the following

Sub Main
    Dim F as new frmLogin
    Dim result as System.Windows.Forms.DialogResult = F.Showdialog()
    F.Close
    If result = System.Windows.Forms.DialogResult.No Then Exit Sub
    Dim mainFrm as new frmMain
    mainFrm.ShowDialog()
End Sub

On your project properties, change the startup object to be Sub Main.

Test the results

Hope this helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.