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);
}