first time i write. (I must work with vb.net and mysql) I have to do a project and I need that when the user logs in, keep the data in the following form. Example, I log in with a user from the login form, the data is correct and it lets me enter another form that would be the main one. The problem I have is that, when logging in, it shows me the data that I bring from the database in a msg box but then in the following form it does not keep them. I had made a module and it was all in public, which worked for me.
Module ModuloUsuarios
Public documento As Integer
Public idusuario As Integer
Public nombre As String
Public apellido As String
Public nivelusuario As String
End Module
But they told me that I had to do it in class. What I have so far is this:
Public Class sesionUsuario
'Documento del usuario
Private Documento_Usuario As Integer
Public Property obtenerDocumento() As Integer
Get
Return Documento_Usuario
End Get
Set(ByVal value As Integer)
Documento_Usuario = value
End Set
End Property
'Tipo de usuario (Nivel identificador)
Private id_Usuario As Integer
Public Property obtenerIdUsuario() As Integer
Get
Return id_Usuario
End Get
Set(ByVal value As Integer)
id_Usuario = value
End Set
End Property
'Nombre de Usuario
Private Nombre_Usuario As String
Public Property obtenerNombre() As String
Get
Return Nombre_Usuario
End Get
Set(ByVal value As String)
Nombre_Usuario = value
End Set
End Property
'Apellido de Usuario
Private Apellido_Usuario As String
Public Property obtenerApellido() As String
Get
Return Apellido_Usuario
End Get
Set(ByVal value As String)
Apellido_Usuario = value
End Set
End Property
'Estado del usuario (Si se encuentra activo o Inactivo)
Private Estado_Usuario As String
Public Property obtenerEstado() As String
Get
Return Estado_Usuario
End Get
Set(ByVal value As String)
Estado_Usuario = value
End Set
End Property
'Email de Usuario
Private Email_Usuario As String
Public Property obtenerEmail() As String
Get
Return Email_Usuario
End Get
Set(ByVal value As String)
Email_Usuario = value
End Set
End Property
End Class
Imports MySql.Data.MySqlClient
Public Class frmLogin
Private DatosUsuario As New sesionUsuario
Private Sub btnIngresar_Click(sender As Object, e As EventArgs) Handles btnIngresar.Click
ConexionSql.Open()
Dim consultaUsuario As String = "select * from usuarios where documento='" & txtDocumento.Text & "' and contrasena ='" & txtContrasena.Text & "';"
Dim existeUsuario As New MySqlCommand(consultaUsuario, ConexionSql)
Dim adapter = New MySqlDataAdapter(existeUsuario)
Dim dt = New DataTable
Dim leer As MySqlDataReader
leer = existeUsuario.ExecuteReader
If leer.HasRows Then
While leer.Read()
DatosUsuario.obtenerDocumento = leer.GetInt32(0)
DatosUsuario.obtenerIdUsuario = leer.GetInt32(2)
DatosUsuario.obtenerNombre = leer.GetString(3)
DatosUsuario.obtenerApellido = leer.GetString(4)
DatosUsuario.obtenerEmail = leer.GetString(9)
DatosUsuario.obtenerEstado = leer.GetString(11)
End While
leer.Dispose()
If DatosUsuario.obtenerIdUsuario < 5 Then
txtDocumento.Text = ""
txtContrasena.Text = ""
MsgBox("Bienvenido/a " + DatosUsuario.obtenerNombre + " " + DatosUsuario.obtenerApellido)
frmPrincipal.Show()
Me.Hide()
Else
DatosUsuario.obtenerIdUsuario = 5
txtDocumento.Text = ""
txtContrasena.Text = ""
MsgBox("Bienvenido/a " + DatosUsuario.obtenerNombre + " " + DatosUsuario.obtenerApellido)
frmClientes.Show()
Me.Hide()
End If
Else
MsgBox("Documento y/o Contraseña Incorrectos")
txtDocumento.Text = ""
txtContrasena.Text = ""
txtDocumento.Focus()
End If
ConexionSql.Close()
End Sub
End Class
This is the Main form (it's a test I'm doing to see if it works, otherwise I'll have to keep the old version of the project uu )
Public Class frmPrincipal
Private sesionUsuario As New sesionUsuario
Public DatosUsuario As New sesionUsuario
Private Sub frmPrincipal_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lblnombre.Text = "hola" + sesionUsuario.obtenerNombre + " " + sesionUsuario.obtenerApellido
End Sub
end class
I need help. I honestly don't know if I'm doing it right or not. I have little time left and well, despair enters: V
From already thank you very much.
**EDIT: Next issue shot... **
I have the main Form which I bring the data, but when I want to open it in a panel (within the same form) I get the following problem In dim frmLoaded I get the error "No argument has been specified for the 'DataUser' parameter of 'Public Sub New(UserData As userSession)' "
Button to bring the frmTecnicos inside a panel in frmPrincipal
Private Sub btnTecnicos_CheckedChanged(sender As Object, e As EventArgs) Handles btnTecnicos.CheckedChanged
'TECNICOS
If Me.PContenedorForm.Controls.Count > 0 Then
Me.PContenedorForm.Controls.RemoveAt(0)
End If
Dim frmCargado As New frmTecnicos()
frmCargado.TopLevel = False
frmCargado.FormBorderStyle = FormBorderStyle.None
frmCargado.Dock = DockStyle.Fill
Me.PContenedorForm.Controls.Add(frmCargado)
Me.PContenedorForm.Tag = frmCargado
frmCargado = New frmTecnicos(DatosUsuario)
frmCargado.Show(DatosUsuario)
End Sub
frmTechnicians:
'Constructor
Public Sub New(ByVal DatosUsuario As sesionUsuario)
InitializeComponent()
Me.DatosUsuario = DatosUsuario
If DatosUsuario.obtenerIdUsuario = 1 Then
txtDescripcionAdministrativo.Enabled = True
txtDiagnostico.Enabled = True
Else
If DatosUsuario.obtenerIdUsuario = 2 Then
txtDescripcionAdministrativo.Enabled = True
txtDiagnostico.Enabled = False
Else
If DatosUsuario.obtenerIdUsuario = 3 Then
txtDescripcionAdministrativo.Enabled = False
txtDiagnostico.Enabled = True
End If
End If
End If
End Sub
A priori your code looks correct. The only thing you would need would be to send the instance
sesionUsuario
that you are completing to the main form.One way would be to declare a public variable in your
frmPrincipal
of typesesionUsuario
:And when creating the form, pass the information:
And do the same with him
frmClientes
.Another option is to pass it in the form constructor:
In the
frmPrincipal
, you create a new constructor:In the Login Form, you must create the main form when you already have the login data loaded:
Dim frmPrincipal as FormPrincipal ' without new .... ... frmPrincipal = New FormPrincipal(DataUser) frmPrincipal.Show() Me.Hide()
In this way, you pass the data to
frmPrincipal
at the time you are creating it.