C
C#2y ago
yumo

❔ Login Sql error

This error displays when i click on my button "entrar" to login with my account can anyone tell me what am i doing wrong?
private void btnEntrar_Click(object sender, EventArgs e)
{
string utilizador = txtUtilizador.Text;
string senha = txtSenha.Text;

if (utilizador == "" || senha == "")
{
MessageBox.Show("Utilizador e ou senha invalidos");
txtUtilizador.Focus();
return;
}

string sql = "SELECT * FROM Utilizadores WHERE utilizador='" + utilizador + "' AND senha='" + senha + "'";
dt = Banco.dql(sql);
if (dt.Rows.Count == 1)
{
mp.lb_acesso.Text = dt.Rows[0].ItemArray[4].ToString();
mp.lb_utilizador.Text = dt.Rows[0].Field<string>("nome");
mp.pictureBox1.Image = Properties.Resources.led_verde;
Globais.nivel = int.Parse(dt.Rows[0].Field<Int64>("nivelUtilizador").ToString());
Globais.logado = true;
this.Close();
}
else
{
MessageBox.Show("Utilizador não encontrado!");
}
}
private void btnEntrar_Click(object sender, EventArgs e)
{
string utilizador = txtUtilizador.Text;
string senha = txtSenha.Text;

if (utilizador == "" || senha == "")
{
MessageBox.Show("Utilizador e ou senha invalidos");
txtUtilizador.Focus();
return;
}

string sql = "SELECT * FROM Utilizadores WHERE utilizador='" + utilizador + "' AND senha='" + senha + "'";
dt = Banco.dql(sql);
if (dt.Rows.Count == 1)
{
mp.lb_acesso.Text = dt.Rows[0].ItemArray[4].ToString();
mp.lb_utilizador.Text = dt.Rows[0].Field<string>("nome");
mp.pictureBox1.Image = Properties.Resources.led_verde;
Globais.nivel = int.Parse(dt.Rows[0].Field<Int64>("nivelUtilizador").ToString());
Globais.logado = true;
this.Close();
}
else
{
MessageBox.Show("Utilizador não encontrado!");
}
}
14 Replies
HimmDawg
HimmDawg2y ago
Start your debugger and check, which variable is null
yumo
yumoOP2y ago
mp.lb_acesso
yumo
yumoOP2y ago
HimmDawg
HimmDawg2y ago
Some kind of label?
yumo
yumoOP2y ago
yeah
HimmDawg
HimmDawg2y ago
How do you set it then? fluffyFoxThink Not in the designer I suppose?
yumo
yumoOP2y ago
nope this is basicly a label on other form mp is main menu so MainMenu.lb_acesso the label is on MainMenu
yumo
yumoOP2y ago
yumo
yumoOP2y ago
this is the form that will load after the login and the label changes to the number i have on my table utilizadores on the column nivelUtilizador
HimmDawg
HimmDawg2y ago
Yeah, that's a bit weird. Accessing controls across forms is usually avoided.
yumo
yumoOP2y ago
its just a label i mean just to show the user what its his acess level
HimmDawg
HimmDawg2y ago
Then you'll need to find out, where this label becomes null. I can only guess, that it unloads or something while you are on the other form
TheRanger
TheRanger2y ago
i dont see what would unload it maybe it wasnt even initialized in the first place
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server