How to prevent duplicity in registration / change of names / company name [closed]

0

In a registry, c # windows form, where I use CPF's or CNPJ's what is the best way to prevent duplication of names / corporate names in inclusion or change.

For example, it may happen that in the change the user informs a name / reason that is already registered.

Using names to search would not be indicated because the spaces between can be interpreted as differentiators eg "William da Silva" and "William da Silva".

What is the best approach to this problem?

    
asked by anonymous 17.12.2014 / 20:57

2 answers

1

The way I use to check is through the following query

select Id from Tabela where Id <> @Id and Cpf = @Cpf

In this way, if the query returns any result, it means that the record already exists in the database.

The @Id parameter is your primary key when it is a new record, its value will be 0 , comparing only the second parameter, which is what you want not to be duplicated, which in my example is @Cpf .

    
17.12.2014 / 21:55
1

Look what I would do in this case is put in the field of your table it as constraint, so the bank would not let you enter a record with same value. He would make a mistake. You can catch this error in Try catch and inform the user.

Try to read about constraint in table fields.

    
18.12.2014 / 00:58