I have a function that does not allow me to enter the same data in the same row. at the time of entering a new product if it works I can not enter a product that is already in the database, the problem if I want to change and I only want to change the sale price example can not because it can not be saved with the same name ANY IDEA?
THIS IS MY SAVE BUTTON
private void btnGuardar_Click(object sender, EventArgs e)
{
try
{
if (modificar)
{
Eproductos editar = new Eproductos();
bool exist = dgvfiltrarproductos.Rows.Cast<DataGridViewRow>().Any(row => Convert.ToString(row.Cells["Producto"].Value) == txtnombre.Text);
bool exist2 = dgvfiltrarproductos.Rows.Cast<DataGridViewRow>().Any(row => Convert.ToString(row.Cells["CodSAC"].Value) == txtCodSAC.Text);
//SI EL VALOR NO EXISTE EN LA BASE DE DATOS LO INGRESA
if (!exist == true)
{
if (!exist2 == true)
{
editar.Codproducto = Convert.ToInt32(txtnombre.Tag.ToString());
editar.Producto = txtnombre.Text;
editar.CodSac = txtCodSAC.Text;
editar.Marca.Codmarca = Convert.ToInt32(txtmarca.Text.ToString());
editar.Existencia = Convert.ToDecimal(txtexistencia.Text.ToString());
editar.Precio = Convert.ToDecimal(txtprecioventa.Text.ToString());
editar.Costo = Convert.ToDecimal(txtpreciocompra.Text.ToString());
if (rbtnActvivo.Checked == true)
{
editar.Activo = true;
}
else
{
editar.Activo = false;
}
Nproductos gestion = new Nproductos();
gestion.modificar(editar);
MessageBox.Show("Se modifico correctamente", "PRODUCTO", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Este CodSAC ya fue ingresado", "REVISE");
}
}
//SI EL VALOR EXISTE EN LA BASE DE DATOS NO LO INGRESA
else
{
MessageBox.Show("Este producto ya fue ingresado", "REVISE");
}
}
else
{
Eproductos nuevo = new Eproductos();
//COMPROBAR SI EL REGISTRO YA EXISTE CON UNA CONSULTA "LINQ" RECORRE TODA LA FILA DEL DATAGRID VIEW Y LA COLUMNA ESPECIFICA .
bool exist = dgvfiltrarproductos.Rows.Cast<DataGridViewRow>().Any(row => Convert.ToString(row.Cells["Producto"].Value) == txtnombre.Text);
bool exist2 = dgvfiltrarproductos.Rows.Cast<DataGridViewRow>().Any(row => Convert.ToString(row.Cells["CodSAC"].Value) == txtCodSAC.Text);
//SI EL VALOR NO EXISTE EN LA BASE DE DATOS LO INGRESA
if (!exist == true)
{
if (!exist2 == true)
{
nuevo.Producto = txtnombre.Text;
nuevo.Marca.Codmarca = Convert.ToInt32(txtmarca.Text.ToString());
nuevo.CodSac = txtCodSAC.Text;
nuevo.Existencia = Convert.ToDecimal(txtexistencia.Text.ToString());
nuevo.Precio = Convert.ToDecimal(txtprecioventa.Text.ToString());
nuevo.Costo = Convert.ToDecimal(txtpreciocompra.Text.ToString());
if (rbtnActvivo.Checked == true)
{
nuevo.Activo = true;
}
else
{
nuevo.Activo = false;
}
Nproductos gestion = new Nproductos();
gestion.agregar(nuevo);
MessageBox.Show("Se guardo correctamente", "PRODUCTO", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
listaproducto.Add(nuevo);
MessageBox.Show("Este CodSAC ya fue ingresado", "REVISE");
}
}
//SI EL VALOR EXISTE EN LA BASE DE DATOS NO LO INGRESA
else
{
listaproducto.Add(nuevo);
MessageBox.Show("Este producto ya fue ingresado", "REVISE");
}
}
ActualizarLista();
Limpiar();
Deshabilitar();
btnGuardar.Enabled = false;
btnCancelar.Enabled = false;
btnModificar.Enabled = false;
btnBuscarmarca.Enabled = false;
btnNuevo.Enabled = true;
modificar = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}