Check if the name is already registered in the Database

Asked

Viewed 61 times

0

This is a Mangáka registration screen, so if I type a name it is added without problems, however I want to check if there is already any record with that name.

Table:

create table tblMangaka(
                        idMangaka int(20) auto_increment primary key,
                        Mangaka varchar(500)
                       );

Code C#, would be at the beginning would do the checking, if text txtNome is already inserted in table tblMangaka.

strMysql = "insert into dbMangas.tblMangaka (Mangaka)values(@Mangaka);";

MySqlCommand comando = new MySqlCommand(strMysql, con);
comando.Parameters.AddWithValue("@Mangaka", txtNome.Text);

try
{
    con.Open();
    comando.ExecuteNonQuery();
}
catch (Exception erro)
{
    MessageBox.Show("Ocorreu um erro:\n" + erro);
}
finally
{
    MessageBox.Show("Mangáka Cadastrado!", "Mangáka", MessageBoxButtons.OK);
    txtNome.Clear();
    txtNome.Focus();
    con.Close();

    inserirCmbMangaka();
}
  • Despite another language, the DB is the same, and the solution is the same - insert and check if there was a violation error of UNIQUE: https://answall.com/a/23027/70 - even has an important warning at the beginning about misuse of SELECT in these cases..

  • There are other problems specific to C#, but I don’t even try anymore because almost everyone learned wrong, it spread in such a way that it doesn’t even have more to try to tidy in people. Anyway researching here has and a lot of information to start creating better codes, I myself answered several times about it.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.