9
On the screen of login, carry out the verification in the bank by means of a select
, and I’m using the catch
to capture this exception.
It is correct to use the catch
for that guy?
if (Usuario != string.Empty && Password != string.Empty)
{
try
{
consql._sql = @"SELECT id_usu FROM login WHERE usuario = @usuario AND password = @password";
//consql._sql = @"SELECT COUNT(id_usu) FROM login WHERE usuario = @usuario AND password = @password";
SqlCommand cmd1 = new SqlCommand(consql._sql, sqlconn);
cmd1.Parameters.Add("@usuario", SqlDbType.VarChar).Value = Usuario;
cmd1.Parameters.Add("@password", SqlDbType.VarChar).Value = Password;
sqlconn.Open();
int count_id = (int)cmd1.ExecuteScalar();
if (count_id > 0)
{
Sessaosistema.UsuarioId = count_id;
Sessaosistema.NomeUsuario = Usuario;
MessageBox.Show("Usuario logado com sucesso", "Login", MessageBoxButtons.OK, MessageBoxIcon.Information);
Menu_Inicial mi = new Menu_Inicial();
mi.Show();
this.Hide();
}
}
catch (Exception)
{
MessageBox.Show("Usuário ou Senha incorretos" + "\n" + "Revise os dados inseridos e tente novamente", "Falha de Logon", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
sqlconn.Close();
}
}
Thanks friend for the explanation. I’ll adjust my code.
– Thomas Erich Pimentel
Good question and great answer. And, just to point out what many programmers do:
Try / Catch
is notIf / Else
– William Aparecido Brandino