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
yumo2y ago
mp.lb_acesso
yumo
yumo2y ago
HimmDawg
HimmDawg2y ago
Some kind of label?
yumo
yumo2y ago
yeah
HimmDawg
HimmDawg2y ago
How do you set it then? fluffyFoxThink Not in the designer I suppose?
yumo
yumo2y ago
nope this is basicly a label on other form mp is main menu so MainMenu.lb_acesso the label is on MainMenu
yumo
yumo2y ago
yumo
yumo2y 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
yumo2y 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.