prowebinteractiveinc
asked on
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:
Form2
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.