1
I am developing a code initially registration and login, everything ok.
I added a Unique Constraint to the database columns that I don’t want you to repeat and it worked fine. However, I would like to know how I can treat this repetition also in the C#code, since, if a person puts a duplicated email for example, I would like the message to appear: "E-mail already registered!".
And another downside, as I didn’t deal with in C#, when I try to insert a repeated die, it doesn’t really enter the bank, okay. But, the ID that is with auto_increment
pula, IE, if it was inserted in the bank until the ID 1, tried to insert 4 times a duplicate data, the next storage in the bank goes to ID 5, as in the image:
The code so far is like this:
public string Cadastrar(string nome, string email, string senha, string confirmarsenha, string celular, string lembretesenha)
{
// checar se campos não estão em branco
if (!email.Equals("") && !senha.Equals("") && !celular.Equals("") && !lembretesenha.Equals(""))
{
// checar se senha e email tem caracteres minimos
if (senha.Length >= 5 && email.Length >= 3)
{
// checar se senha é igual a confirmar senha
if (senha.Equals(confirmarsenha))
{
comando.CommandText = "insert into funcionario(nome, email, senha, celular, lembretesenha)values(@nome, @email, @senha, @celular, @lembretesenha);";
comando.Parameters.AddWithValue("@nome", nome);
comando.Parameters.AddWithValue("@email", email);
comando.Parameters.AddWithValue("@senha", senha);
comando.Parameters.AddWithValue("@celular", celular);
comando.Parameters.AddWithValue("@lembretesenha", lembretesenha);
check = false;
try
{
comando.Connection = conect.Conectar();
comando.ExecuteNonQuery();
conect.Desconectar();
this.mensagem = "Cadastrado com sucesso!";
check = true;
}
catch (SqlException)
{
this.mensagem = "Erro com o banco de dados!";
}
}
else
{
MessageBox.Show("As senhas devem ser iguais!");
}
}
else
{
MessageBox.Show("Por favor, use uma senha com mais de 5 caracteres!");
}
}
else
{
MessageBox.Show("Por favor, não deixe campos em branco!");
}
return mensagem;
}
Following its Programming structure. You could create a method to check if the user is already registered. In case he’s already here, you wouldn’t call his routines criminal record.
– Gabriel Santana