Question show username who login

trialer

Well-known member
Joined
Oct 4, 2010
Messages
64
Programming Experience
1-3
after logging in i want to see
example:

Username: [Username]
Department: [Department]
 
You've already got the user name because the user provided it to you when they logged in. In order to validate the credentials provided you must be querying the database, so you simply retrieve the department in that query. If you're still not sure then please show us the code you're using to authenticate the user at the moment.
 
what do you mean ??

Hi trialer
I would be better if you can write your problem more clearly.
What is your problem actually, is it :

* You are providing user login form and validate the credentials (username and pass) by accessing database ?

If yes, then the answer would be very simple, just take the info taken from user controls such as textbox or whatever you used, cache them in a variable -> do the validation with the database -> show the cached info after validation were successfull
 
loginhl.jpg


menub.th.jpg


My login form is working.
it is connected to sql database.

Now i want to see my login name in the toolstripstatus at the bottom
Username: [username]


thanks..
 
As has been said, you've already got the user name because the user typed it into the TextBox on the login form. Just have the main form get the Text from that TextBox, either directly or via a property that you add, and then assign it to the Text of the appropriate ToolStripStatusLabel. As has also been said, if you want other user information then you need to retrieve that using the same query that validates your login. You can then treat that the same as the user name. I did say:
If you're still not sure then please show us the code you're using to authenticate the user at the moment.
and you're still asking for help yet we are yet to see that code. If you're not going to provide the information we ask for then you make it hard for us to help you.
 
Code for Login Form

VB.NET:
Imports System.Data.SqlClient


Public Class frmLogin
    Private success As Boolean
    Private counter As Byte = 0
    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        Connect()
        Dim cmd As New SqlCommand("Select username,password from table_login where username='" & txtUsername.Text & "'", con)
        Dim dr As SqlDataReader = cmd.ExecuteReader
        If dr.Read Then
            If txtPassword.Text = dr("password") Then
                dr.Close()
                'success = True
                Me.Hide()
                frmMenu.Show()
            Else
                'success = False
                counter += 1
                MessageBox.Show("Invalid password.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
                If counter = 3 Then
                    Me.Close()
                End If
            End If
        Else
            success = False
            MessageBox.Show("Username does not exist.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
        dr.Close()

    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        Me.Close()
    End Sub
End Class


Code for the MDI Parent

VB.NET:
Public Class frmMenu

    Private Sub LoginToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoginToolStripMenuItem.Click
        frmLogin.ShowDialog()
    End Sub

    Private Sub RegistrationToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RegistrationToolStripMenuItem.Click
        frmRegistration.MdiParent = Me
        frmRegistration.Show()
    End Sub
    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Disconnect()
        Me.Close()
    End Sub

    Private Sub frmMenu_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Dim close As String
        close = MessageBox.Show("Are you sure you want to exit?", "Exit RCBS", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If close = 7 Then
            e.Cancel = True
        Else
            e.Cancel = False
        End If
    End Sub
End Class
 
Firstly, you have to make sure that after adding a toolstrip at your form, you already create an item inside it.
If you want to show some status or other text, then it would be label item.
After that it's easy

If txtPassword.Text = dr("password") Then
dr.Close()
'success = True
frmMenu.toolstrip.items(toolstripstatus).text = textusername.text
Me.Hide()
frmMenu.Show()
Else
'success = False
counter += 1
MessageBox.Show("Invalid password.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
If counter = 3 Then
Me.Close()
End If
End If

Hope this help
 
Firstly, you have to make sure that after adding a toolstrip at your form, you already create an item inside it.
If you want to show some status or other text, then it would be label item.
After that it's easy

If txtPassword.Text = dr("password") Then
dr.Close()
'success = True
frmMenu.toolstrip.items(toolstripstatus).text = textusername.text
Me.Hide()
frmMenu.Show()
Else
'success = False
counter += 1
MessageBox.Show("Invalid password.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
If counter = 3 Then
Me.Close()
End If
End If

Hope this help

error: Object variable or With block variable not set.
 
Please note within this code

frmMenu.toolstrip.items(toolstripstatus).text = textusername.text

is within assumption that :
1. Your parent MDI form, which is the form where toolstrip control exist named as 'frmMenu'.
2. Your toolstrip control named as 'toolstrip'
3. The label item inside the toolstrip control (where you want the user name displayed) named as 'toolstripstatus'

If not, then please change the varibable names as you use
 
trialer said:
VB.NET:
Private Sub LoginToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoginToolStripMenuItem.Click
        frmLogin.ShowDialog()
    End Sub
could change to:
VB.NET:
If frmLogin.ShowDialog() = DialogResult.OK Then
    'user name = frmLogin.txtUsername.Text
End If
Set the appropriate DialogResult in frmLogin dialog.

This is how one usually use dialogs.
 
could change to:
VB.NET:
If frmLogin.ShowDialog() = DialogResult.OK Then
    'user name = frmLogin.txtUsername.Text
End If
Set the appropriate DialogResult in frmLogin dialog.

This is how one usually use dialogs.


it works. kindly closed this thread
question answered.

thanks!
 
I believe that the Login Information can be show on other Windows Form...
Firstly, i set the 【form_login】 Me.Hide() , information still there....
Secondly, i call the 【form_login】 information to other Windows Form by writing form_login.txt_username.Text

VB.NET:
Imports System.Data.SqlClient

Public Class form_login

    Private Sub btn_login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_login.Click
        Try
            Dim con As New SqlConnection
            Dim cmd As New SqlCommand
            Dim rd As SqlDataReader

            con.ConnectionString = (connection)
            cmd.Connection = con
            con.Open()
            cmd.CommandText = "SELECT staff_name,staff_pw FROM staff_info WHERE staff_name= (@name) AND staff_pw= (@pw) "
            cmd.Parameters.AddWithValue("@name", txt_username.Text)
            cmd.Parameters.AddWithValue("@pw", txt_password.Text)

            rd = cmd.ExecuteReader

            If rd.HasRows Then
                Me.Hide()
                form_main.ShowDialog()
            Else
                MessageBox.Show("Invalid Login or Password")
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Error")
        End Try
    End Sub
End Class


'-------------------------------------------------------------------------------------------

Public Class form_main

    Private Sub form_main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        timer.Enabled = True
[B]        lblusername.Text = form_login.txt_username.Text[/B]
    End Sub

    Private Sub timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer.Tick
        nowdatetime.Text = Date.Now.ToString("dd-MM-yyyy  hh:mm:ss")
    End Sub

End Class
 
Last edited by a moderator:
I believe that the Login Information can be show on other Windows Form...
Firstly, i set the 【form_login】 Me.Hide() , information still there....
Secondly, i call the 【form_login】 information to other Windows Form by writing form_login.txt_username.Text

VB.NET:
Imports System.Data.SqlClient

Public Class form_login

    Private Sub btn_login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_login.Click
        Try
            Dim con As New SqlConnection
            Dim cmd As New SqlCommand
            Dim rd As SqlDataReader

            con.ConnectionString = (connection)
            cmd.Connection = con
            con.Open()
            cmd.CommandText = "SELECT staff_name,staff_pw FROM staff_info WHERE staff_name= (@name) AND staff_pw= (@pw) "
            cmd.Parameters.AddWithValue("@name", txt_username.Text)
            cmd.Parameters.AddWithValue("@pw", txt_password.Text)

            rd = cmd.ExecuteReader

            If rd.HasRows Then
                Me.Hide()
                form_main.ShowDialog()
            Else
                MessageBox.Show("Invalid Login or Password")
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Error")
        End Try
    End Sub
End Class


'-------------------------------------------------------------------------------------------

Public Class form_main

    Private Sub form_main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        timer.Enabled = True
[B]        lblusername.Text = form_login.txt_username.Text[/B]
    End Sub

    Private Sub timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer.Tick
        nowdatetime.Text = Date.Now.ToString("dd-MM-yyyy  hh:mm:ss")
    End Sub

End Class

It works but it's a poor solution. Hiding a form rather than closing it so that you can use data from it later is dodgy.
 
Back
Top