Good day programmers!.
i have here a login in form1 that is connected to my sql database.
i want that if i login in form1. the form2 will show my name.
can anyone help me?.

4 Years
Discussion Span
Last Post by smitty68503

You can:

  • Create a global variable that will hold your username and use it whenever you need it. In your case you'd query the db for info for that username.
  • You can get all the info you want in the login sub and pass it to the second form like parameters.

When you open form2, you only have to pass variable to constructor of form, and pas it to some control, which will show the value (ie. username):

    'on form1:
    Public Property YourUserName() As String
            Return m_YourUserName
        End Get
        Private Set
            m_YourUserName = Value
        End Set
    End Property
    Private m_YourUserName As String
    'this is the variable you pass a value from database when logging in

    Private Sub buttonOpenForm2_click(sender As Object, e As EventArgs)
        Dim f2 As New Form2(YourUserName)
    End Sub

    'on form2:
    Public Sub New()
        'constructor 1
    End Sub

    Public Sub New(UserName As String)
        'constructor 1
        'show username in label control:
        Me.label1.Text = UserName
    End Sub

Edited by pyTony: fencing ~~~ vb code tags added


simple way is to first have a sql database created. then once you have done that all you have to do is drag and drop an sqladapter. then follow the steps that basically link to your database. once that is one test to see the connection is working. then you have to select the tables that you are linking to and do the following:


select username,passwrd from login_details

where(username=@Uname) and (passowrd=@pass)

once you have done that execute it. Now create a dataset from the adapter.
then you do the coding in your form like so:

 Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
    If (txtUName.Text = "-") Or (txtUName.Text = "0") Or (txtUName.Text = "") Or (txtPass.Text = "0") Or (txtPass.Text = "-") Or (txtPass.Text = "") Then
        MessageBox.Show("Invalid Entry", "ErrorMessageBox", MessageBoxButtons.OK, MessageBoxIcon.Error)
        txtUName.Text = ""
        txtPass.Text = ""
    End If
    unameid = txtUName.Text
    sqldLoginadapter.SelectCommand.Parameters("@Uname").Value = txtUName.Text
    sqldLoginadapter.SelectCommand.Parameters("@pass").Value = txtPass.Text
    sqldLoginadapter.Fill(DatasetLogin1, "tablename")

    If (DatasetLogin1.Tables("tablename").Rows.Count > 0) Then
        MessageBox.Show("Login Successfull! Welcome Employee: " & empeid, "MessageBox", MessageBoxButtons.OK, MessageBoxIcon.Information)
        txtUName.Text = ""
        txtPass.Text = ""
        MessageBox.Show("Not Registered Employee Details!. Please Try Again", "ErrorMessageBox", MessageBoxButtons.OK, MessageBoxIcon.Error)
        txtUName.Text = ""
        txtPass.Text = ""
    End If
End Sub


Edited by pyTony: rubbish templates removed


You can also use the Active Directory if you are wanting a full name. That would be the most accurate way if you were writing
a multiple user application. Each user's name would be in teh active directory.


u can use the below code

this code shud be written in form1 where u will redirect it to form2

form2.txtName.Text = txtUsername.Text

else u can use the below code in ur form2 load event

txtName.Text = form1.txtUsername.Text

I am doing something similar here and have it down as far as passing from form1 to form2. However, I am looking to go a step further and display the person's first and last name as well. I understand I will have to take that from the database based on the login info. However I am lost on how to go about it. Thanks for any help.


u can have a select query where the username will be the one from the login screen...according to the username get the firstname and the last name and display it on the screen....this can be done in the Login button...

while validating the login u can write as below....

select firstname, lastname, password from tablename where username ='" +Textbox1.Text+"'"

in this way u will get the firstname and lastname as well....

so first validate the password and then move the firstname and lastname in some variable and then show that variable on form 2 as soon as login is validated...

Dim fname as String
Dim lname as String

and then let the reader read the value


form2.txtName.Text = fname + lname 'this will show the variable on form2

Again, I suggest the Active Directory.

The user's first and last name will be stored in the active directory.

You can retrieve this by capturing the username that is logged into the application.

'Declare a public variable for the full name.'
Public FullName As String

'Call the function and assign the return to the FullName variable.'
FullName = GetRealNameFromAD(Environment.UserName)

'This is the function to drop in.'
Private Function GetRealNameFromAD(ByVal UsernameToFind As String) As String
    'Searches the active directory to get the users real name.'
    'Uses the user who is logged on at that time.'
    Dim full as String
        Using searcher As New DirectorySearcher(New DirectoryEntry())
            searcher.PageSize = 1000
            searcher.SearchScope = SearchScope.Subtree
            searcher.Filter = "(&(samAccountType=805306368)(sAMAccountName=" & UsernameToFind & "))"
            Using Results As SearchResultCollection = searcher.FindAll
                If Results Is Nothing OrElse Results.Count <> 1 Then
                    Throw New ApplicationException("Invalid number of results returned - either no users were found or more than one user account was found")
                End If
                Using UserDE As DirectoryEntry = Results(0).GetDirectoryEntry
                    full = CStr(UserDE.Properties("givenName").Value) & " " & CStr(UserDE.Properties("sn").Value)
                    return full
                End Using
            End Using
        End Using
    Catch ex As Exception
        MsgBox("Exception from: " & ex.Source & vbCrLf & ex.Message & vbCrLf & "Unable to lacate the user in active directory.", MsgBoxStyle.OkOnly)
    End Try
End Function

Edited by Begginnerdev: Fixing bad comment tags


Thanks Pooj. Your post was very helpful. However, I am using code similar to yours and it isn't pulling anything from the database at all. It errors out saying it didn't find any data there. I am going to keep working on this and find the answer. I love this forum. It is very friendly.

Label2.Text = PassedText
        Label4.Text = TimeOfDay
        Dim conn As New SqlConnection("*****")
        Dim cmd As SqlCommand = New SqlCommand("SELECT * FROM {TableName} WHERE Username = '" & Label2.Text & " '", conn)
        Dim sdr As SqlDataReader = cmd.ExecuteReader()
        If (sdr.Read() = True) Then
            Dim fname As String
            Dim lname As String
            fname = sdr("FirstName")
            lname = sdr("LastName")
            Label8.Text = fname & "  " + lname
            Label2.Visible = False

I figured it out. I'm not so sure it is "expert" material but it worked. I just passed the username.text to the next form and used that to pull the rest of the information. You were a great help in that. I really appreciate it and appreciate your help. Thanks a ton.

I understand Active Directory would be more efficient and would probably be better but I am a small company and really don't need to be that efficient. lol

Edited by pyTony: fencing ~~~ vb code tags added

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.