I was able to get the user logged in using typed text in TextBox
.
But I do not want the user, I want the username.
Database example:
id = 1
nome_usuario = Administrador
usuario = admin
senha = 1234
nivel_acesso = 1
I want to get the data nome_usuario
and display it in a Label on the main form of my application.
This is my form code frmLogin
public partial class frmLogin : Form
{
public bool logado = false;
public static string usuarioConectado;
public frmLogin()
{
InitializeComponent();
}
private void btnEntrar_Click(object sender, EventArgs e)
{
string conexao = "Data Source=DESKTOP-AJLR3DB\SQLEXPRESS;Initial Catalog=DBGestor;Integrated Security=True";
SqlConnection conn = new SqlConnection(conexao);
SqlCommand comando = new SqlCommand("SELECT * FROM funcionarios WHERE usuario = @usuario and senha = @senha", conn);
comando.Parameters.Add("@usuario", SqlDbType.NVarChar).Value = txtUsuario.Text;
comando.Parameters.Add("@senha", SqlDbType.Int).Value = txtSenha.Text;
conn.Open();
int i = (int)comando.ExecuteScalar();
if (i > 0)
{
logado = true;
usuarioConectado = reader["nome_funcionario"].ToString();
this.Dispose();
}
else
{
logado = false;
MessageBox.Show("Usuário e/ou Senha inválido.");
}
conn.Close();
}
private void txtSenha_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsDigit(e.KeyChar) && e.KeyChar != (char)8)
{
e.Handled = true;
MessageBox.Show("Este campo aceita apenas números.");
}
}
private void btnCancelar_Click(object sender, EventArgs e)
{
this.Close();
}
}
I need to know how to assign the value nome_usuario
to the variable usuarioConectado
This is my form code frmPricipal
private void frmPrincipal_Load(object sender, EventArgs e)
{
lblUsuario.Text = "Olá, " + frmLogin.usuarioConectado;
}
I was able to resolve the problem, updated code.