Event TextChange only executes instructions inside else: c # and asp.net

0

I have 4 textBox, I make a query to my BD and depending on what is selected in a DropDownList I must execute a multiplication either Txt1 * Txt3 or Txt2 * Txt3 and show me the result in Txt4 .

The method is the same for all txtBox:

 protected void TxtImporte_TextChanged(object sender, EventArgs e)
{
    OdbcConnection cd = new OdbcConnection("xxxxx");
    cd.Open();
    DataTable dtc = new DataTable();
    OdbcCommand cmd = new OdbcCommand("SELECT T0.ItemName FROM OITM T0 WHERE T0.TreeType = 'T' and T0.ItmsGrpCod in (110,119) and T0.ItemName Like'%?' group by T0.ItemName", cd);
    cmd.Parameters.AddWithValue("codigo", DropDownList1.SelectedItem.ToString());
    OdbcDataAdapter da = new OdbcDataAdapter(cmd);
    da.Fill(dtc);
    if (dtc.Rows.Count == 1)
    {
        Double sueldo = 0;
        Double.TryParse(TxtPiezas.Text, out sueldo);

        Double remuneracion = 0;
        Double.TryParse(TxtPrecio.Text, out remuneracion);

        TxtImporte.Text = (sueldo * remuneracion).ToString("N2");

    }
    else
    {
        Double kilos = 0;
        Double.TryParse(TxtKilos.Text, out kilos);

        Double remuneracion = 0;
        Double.TryParse(TxtPrecio.Text, out remuneracion);

        TxtImporte.Text = (kilos * remuneracion).ToString("N2");
    }
}

But when I run the page, it only executes the instruction after else for all the elements of the DropDownList. My query is well formulated because if I run from sql server it tells me if the product belongs to the range (110,119) or if it does not exist.

In SQL server:

SELECT T0.ItemName FROM OITM T0 WHERE T0.TreeType = 'T' and T0.ItmsGrpCod in (110,119)

Some values are always repeated.

    
asked by Elizabeth 24.07.2018 в 16:50
source

1 answer

0

For local objects I do not like to use a DataAdapter to read information, especially if the datatable is going to be temporary and local (Much Architecture for something very simple)

instead of simple readings I prefer to use a datareader or an ExecuteScalar

     OdbcCommand cmd = new OdbcCommand("SELECT T0.ItemName FROM OITM T0 WHERE T0.TreeType = 'T' and T0.ItmsGrpCod in (110,119) and T0.ItemName Like'%?' group by T0.ItemName", cd);



    OdbcDataReader reader = cmd.ExecuteReader();
    bool hayDatos=reader.HasRows; //para saber si hay resultados
    while (reader.Read())
    {
        //Obtuvimos Datos!, ahora a extraerlos 
        //como es odbc puede que hasrows nos diga false hasta que ejecutemos el metodo Read
          hayDatos= true;
          reader["T0.ItemName"].ToString();// (aunque recomendaria ponerle alias al resultado por que esto puede que falle dependiendo del driver...
    }


if (hayDatos == true )
{
    Double sueldo = 0;
    Double.TryParse(TxtPiezas.Text, out sueldo);

    Double remuneracion = 0;
    Double.TryParse(TxtPrecio.Text, out remuneracion);

    TxtImporte.Text = (sueldo * remuneracion).ToString("N2");

}
else
{
    Double kilos = 0;
    Double.TryParse(TxtKilos.Text, out kilos);

    Double remuneracion = 0;
    Double.TryParse(TxtPrecio.Text, out remuneracion);

    TxtImporte.Text = (kilos * remuneracion).ToString("N2");
}
    
answered by 24.07.2018 / 17:55
source