Avoid duplicate records in C # and SQL Server

0

I have this code to save records,

  • when it is new, enter the record,

  • when the record is repeated it enters foreach and displays the message (the record already exists) but it enters the record anyway .

Is there any way that, when you send the message that the record already exists, quit the cycle and do not keep the record in the database and can enter again?

private void btnGuardar_Click_1(object sender, EventArgs e)
        {
            try
            {
                string rpta = "";
                if (this.txtPais.Text == string.Empty)
                {
                    MensajeError("Falta ingresar algunos datos, serán remarcados");
                    errorIcono.SetError(txtPais, "Ingrese una País");
                    this.txtPais.Focus();
                }
                else
                {
                    if (this.IsNuevo)

                    // Evitar que se registren registros duplicados

                    {                        
                        DataTable tabla = NPais.Mostrar();
                        if (tabla.Rows.Count >= 0)
                        {
                            foreach (DataRow fila in tabla.Rows)
                            {
                                if (fila["nombre"].ToString() == txtPais.Text.Trim().ToUpper())
                                {
                                    MessageBox.Show("El Registro ya Existe!!","Sistema de Ventas", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    break;
                                }
                            }                            
                        }
                        else
                        {
                            MessageBox.Show("No ha ingresado valores!!");
                        }

                        rpta = NPais.Insertar(this.txtPais.Text.Trim().ToUpper());
                    }


                    else
                    {
                        rpta = NPais.Editar(Convert.ToInt32(this.txtIdpais.Text), this.txtPais.Text.Trim().ToUpper());
                    }

                    if (rpta.Equals("OK"))
                    {
                        if (this.IsNuevo)
                        {
                            this.MensajeOk("Se Insertó de forma correcta el registro");
                        }
                        else
                        {
                            this.MensajeOk("Se Actualizó de forma correcta el registro");
                        }
                    }
                    else
                    {
                        this.MensajeError(rpta);
                    }

                    this.IsNuevo = false;
                    this.IsEditar = false;
                    this.Botones();
                    this.Limpiar();
                    this.Close();

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
            }
    
asked by Pancho Haro 13.12.2017 в 03:28
source

1 answer

2

The problem is not so much at the end of foreach , as in the situation of your INSERT . Being out of any conditional, you will always be executed.

One possible way to solve it would be

if (this.IsNuevo)

    // Evitar que se registren registros duplicados
    {                        
        DataTable tabla = NPais.Mostrar();
        if (tabla.Rows.Count >= 0)
        {
            repetido = false;
            foreach (DataRow fila in tabla.Rows)
            {
                if (fila["nombre"].ToString() == txtPais.Text.Trim().ToUpper())
                {
                    MessageBox.Show("El Registro ya Existe!!","Sistema de Ventas", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    repetido = true;                                        
                    break;
                }
            }                            
        }
        else
        {
            MessageBox.Show("No ha ingresado valores!!");
        }
        if(!repetido)
        {
            rpta = NPais.Insertar(this.txtPais.Text.Trim().ToUpper());
        }
    }
}

The only thing I have modified is to add a variable, which I have called repetido , and that if it is duplicated I will put it to TRUE . Once all the checks have been completed, if this variable is TRUE , it will not make the INSERT , in case it is FALSE if it does it to you

    
answered by 13.12.2017 в 08:37