Like: validate through textBox the existence of a record in the database. C #

0

I would like to know:

How to validate from a textBox the existence of a record stored in the database?

That is, write in the textBox the name of the record and when you press the "Search" button, I get a message that tells me that the record exists.

This is what I have done.

And it's in my "Search" button:

        using (ProductosEF _bdVentas = new ProductosEF())
        {
            Producto _Producto = new Producto();
            var BuscarProducto = _bdVentas.BusquedaProductos(textBox8.Text.Trim()).ToList();

                txtcodei.Text = BuscarProducto.FirstOrDefault().codigo_interno;
                txtcodebar.Text = BuscarProducto.FirstOrDefault().codigo_barra;
                txtdescripcion.Text = BuscarProducto.FirstOrDefault().descripcion;
                txtmarca.Text = BuscarProducto.FirstOrDefault().marca;
                txttamaño.Text = BuscarProducto.FirstOrDefault().tamaño;
                txtcosto.Text = BuscarProducto.FirstOrDefault().costo.ToString();
                txtprecio.Text = BuscarProducto.FirstOrDefault().precio.ToString();

It works like this:

I write the code in the textBox, I press the button, and then it brings me the records associated with the code. What I want to know is:

As: Valid that, if the code exists, show me a message of existence, otherwise I enable my corresponding fields to create the new product.

    
asked by Dave 08.02.2018 в 16:04
source

3 answers

0

First you make a query to the database with the TextBox data and then:

       public bool ValidarProducto(BuscarProducto bp)
    {
      bool existe=false;
     try
    {
         if(bp.count()>0)
         {
           existe=true;
         }
    }
    Catch(Exception ex)
    {
      throw;
    }
  return existe;
  }

After you return the Boolean you can see what samples as a message for the user to know.

if(ValidarProducto(buscproc))
{
  messagebox.show("El producto ya existe pruebe con un código diferente.")
}
    
answered by 12.02.2018 / 22:09
source
0

What happens is that you do not say if you already have the method for the edition and that you use it to edit the data if they are textBox and an edit button you can do the following:

if (BuscarProducto.Lengt ==0) {
     //no encontro habilitar para insercion
     textBox1.ReadOnly = false;
     //suponiendo que tienes todo esto desactivado por que no explicas y asi activas todos tus edit textBox y puedes habilitar el boton de editar 
    button1.Enabled = true;
    MessageBox.Show("El producto ya existe");
    //y ya llenas el formulario y cuando lo edites los desactivas en el boton de agregar registro.
    } 
    else { 
     // encontro registros mostrarlos para edicion si se requiere
     textBox1.ReadOnly = false;
     //suponiendo que tienes todo esto desactivado por que no explicas y asi 
     activas todos tus edit textBox y puedes habilitar el boton de editar 
    button2.Enabled = true;
    MessageBox.Show("El producto no existe");
    //y ya llenas el formulario y cuando lo edites los desactivas en el boton de editar
    }

I do not know if it would help you because what I understood was what you wanted that was

    
answered by 09.02.2018 в 17:37
0

You can do it in the following way at the moment you click on your button to search for a method that performs this action and returns a Boolean if it is not in the query, I already have something simplified but I share it with you I hope it serves you something if you have questions tell me .. this method receives an SQL query with a Count (*)

So I use it

if (Existe("SELECT COUNT(*) FROM TU_TABLA) == false)
{
// LOGICA 
}

Method:

Method

public static bool Existe(string txtSQL)
    {
        try
        {
            SqlCommand cmd = new SqlCommand(txtSQL, Conex);
            Conectar();
            int count = Convert.ToInt32(cmd.ExecuteScalar());
            if (count == 0)  
            {
                Conex.Close();
                return false;
            }
            else
            {
                Conex.Close();
                return true;
            }
        }
        catch (Exception ex)
        {
            Conex.Close();
            return false;   
        }
    }
    
answered by 09.02.2018 в 19:50