Search after LostFocus, find but do not bring me the data

1

Good evening friends I am speeding up one of my processes in a system, since I have a form ( frmventas ) that has several TextBox like: txtidarticulo , txtnombre , txtcantidad , txtprecio between others. in txtidarticulo there is a button next to btnbuscar , this calls a form ( frmMostrar_articulos ) with a grid a combobox and a textbox where the combobox has two search options: código , nombre and botón only filters what is selected with what it says in the textbox , shows it in the grid and when giving dobleclic to a data load the rest of the information to the text box mentioned above ( frmventas ). as they see for practice a person who goes to look for 10 products is very long the process. I am trying to do the same in the event LostFocus of txtidarticulo find them but do not put the information in the other text boxes of your same form. this is what I have: the first layer:

public DataTable MostrarArticulo_Venta_codigo(String TextoBuscar)
{
    DataTable DtResultado = new DataTable("articulos");
    SqlConnection SqlCon = new SqlConnection();
    try
    {
        SqlCon.ConnectionString = Conexion.Cn;
        SqlCommand SqlCmd = new SqlCommand();
        SqlCmd.Connection = SqlCon;
        SqlCmd.CommandText = "spbuscararticulo_venta_codigo";
        SqlCmd.CommandType = CommandType.StoredProcedure;

        SqlParameter ParTextoBuscar = new SqlParameter();
        ParTextoBuscar.ParameterName = "@textobuscar";
        ParTextoBuscar.SqlDbType = SqlDbType.VarChar;
        ParTextoBuscar.Size = 50;
        ParTextoBuscar.Value = TextoBuscar;
        SqlCmd.Parameters.Add(ParTextoBuscar);

        SqlDataAdapter SqlDat = new SqlDataAdapter(SqlCmd);
        SqlDat.Fill(DtResultado);

    }
    catch (Exception ex)
    {
        DtResultado = null;
    }
    return DtResultado;

}

second layer:

public static DataTable MostrarArticulo_Venta_Codigo(string textobuscar)
{
    DVenta Obj = new DVenta();
    return Obj.MostrarArticulo_Venta_codigo(textobuscar);
}

and the third layer event that cbbuscar you see was to play dirty put text: "code" and visible = false and thanks to that is looking but I know it is not the correct practice :

private void txtIdarticulo_LostFocus(object sender, System.EventArgs e)
{
    if (cbBuscar.Text.Equals("Codigo"))
    {
        this.MostrarArticulo_Venta_Codigo();
    }

    FrmVenta form = FrmVenta.GetInstancia();
        string par1, par2;
        decimal par3, par4;
        int par5;
        DateTime par6;
        par1 = Convert.ToString(this.dataListado.CurrentRow.Cells["iddetalle_ingreso"].Value);
        par2 = Convert.ToString(this.dataListado.CurrentRow.Cells["nombre"].Value);
        par3 = Convert.ToDecimal(this.dataListado.CurrentRow.Cells["precio_compra"].Value);
        par4 = Convert.ToDecimal(this.dataListado.CurrentRow.Cells["precio_venta"].Value);
        par5 = Convert.ToInt32(this.dataListado.CurrentRow.Cells["stock_actual"].Value);
        par6 = Convert.ToDateTime(this.dataListado.CurrentRow.Cells["fecha_vencimiento"].Value);
        form.setArticulo(par1, par2, par3, par4, par5, par6);

}




 public void setArticulo (string iddetalle_ingreso,string nombre,
        decimal precio_compra,decimal precio_venta,int stock,
        DateTime fecha_vencimiento)
    {
        this.txtIdarticulo.Text = iddetalle_ingreso;
        this.txtArticulo.Text = nombre;
        this.txtPrecio_Compra.Text = Convert.ToString(precio_compra);
        this.txtPrecio_Venta.Text = Convert.ToString(precio_venta);
        this.txtStock_Actual.Text = Convert.ToString(stock);
        this.dtFecha_Vencimiento.Value = fecha_vencimiento;
    }

after the breakpoint:

[

but the DataTable is as if it were not active, something else change the event to TextChanged because in lostFocus it was not even activated but it has to be solved one thing at a time

    
asked by Gilberto Asuaje 11.12.2016 в 07:22
source

1 answer

0

First you must validate that your DataTable contains data.

Modify your code as follows:

private void txtIdarticulo_LostFocus(object sender, System.EventArgs e)
{
    if (cbBuscar.Text.Equals("Codigo"))
    {
        this.MostrarArticulo_Venta_Codigo();
    }

    FrmVenta form = FrmVenta.GetInstancia();
    string par1, par2;
    decimal par3, par4;
    int par5;
    DateTime par6;

    // Comprueba primero que tu datatable tenga datos:
    if (dataListado != null && dataListado.Rows.Count > 0) 
    {
        par1 = Convert.ToString(this.dataListado.CurrentRow.Cells["iddetalle_ingreso"].Value);
        par2 = Convert.ToString(this.dataListado.CurrentRow.Cells["nombre"].Value);
        par3 = Convert.ToDecimal(this.dataListado.CurrentRow.Cells["precio_compra"].Value);
        par4 = Convert.ToDecimal(this.dataListado.CurrentRow.Cells["precio_venta"].Value);
        par5 = Convert.ToInt32(this.dataListado.CurrentRow.Cells["stock_actual"].Value);
        par6 = Convert.ToDateTime(this.dataListado.CurrentRow.Cells["fecha_vencimiento"].Value);
        form.setArticulo(par1, par2, par3, par4, par5, par6);
    }
    else 
    {
        MessageBox.Show("No hay datos.");
    }   
}

I will edit this answer to try to help you.

    
answered by 15.12.2016 / 14:37
source