We help IT Professionals succeed at work.
Get Started

passing a variable between Form1 & Form2

937 Views
Last Modified: 2012-08-12
I have created a login screen (Form1) in Form 1 Im trying to pass the users ID, and display his name on Form2, here is what I have

Form1:
Imports MySql.Data.MySqlClient
Public Class Form1
    Public mUserId As String
    Public loginName As String
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub


    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim MysqlConn As MySqlConnection
        MysqlConn = New MySqlConnection()
        Dim dr As MySqlDataReader
        Dim cmd As New MySqlCommand


        MysqlConn.ConnectionString = "server=MyDomain.com;" _
            & "user id=vbnet;" _
            & "password=password;" _
            & "database=admin"

        Try
            MysqlConn.Open()

            cmd.CommandText = "SELECT firstName, lastName FROM profiles WHERE username = ?UserName AND password = ?Password"
            cmd.Parameters.Add(New MySqlParameter("?MID", txtMID.Text))
            cmd.Parameters.Add(New MySqlParameter("?UserName", txtUserName.Text))
            cmd.Parameters.Add(New MySqlParameter("?Password", txtPassword.Text))
            cmd.Connection = MysqlConn
            dr = cmd.ExecuteReader

            If dr.HasRows() Then
                Form2.Show()
                Me.Hide()
                dr.Read()

                loginName = dr(0) & " " & dr(1)
                'MessageBox.Show(loginName) // THIS WORKS OBVIOUSLY
            Else
                Label5.Text = "Invalid Username and/or Password."
                txtPassword.ResetText()
                txtUserName.ResetText()
                txtUserName.Focus()

            End If

            MysqlConn.Close()
        Catch myerror As MySqlException
            MessageBox.Show("Cannot connect to database: " & myerror.Message)
        Finally
            MysqlConn.Dispose()

        End Try

    End Sub

    Private Sub SetupToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetupToolStripMenuItem.Click
        setupForm.Show()
    End Sub

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

Open in new window


Form2
Public Class Form2

    Public Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MessageBox.Show(Form1.loginName) // THIS IS EMPTY ?
        'Label3.Text = Form1.loginName
    End Sub
    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
        Form1.Close()
    End Sub

End Class

Open in new window

Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE