[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 915
  • Last Modified:

passing a variable between Form1 & Form2

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

0
prowebinteractiveinc
Asked:
prowebinteractiveinc
1 Solution
 
PryratesCommented:
You should switch from
              Form2.Show()
                Me.Hide()
                dr.Read()

                loginName = dr(0) & " " & dr(1)
                'MessageBox.Show(loginName) // THIS WORKS OBVIOUSLY

Open in new window

to
                loginName = dr(0) & " " & dr(1)
              Form2.Show()
                Me.Hide()
                dr.Read()

                'MessageBox.Show(loginName) // THIS WORKS OBVIOUSLY

Open in new window


You variable should be assigned to a value before you show your form.
Then everything should be fine.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now