I must consult a field in a table and see if there is a value

1

In a table I have the CM1_CUENTA field where a user can have many accounts but I need to know if among all of them exists the numbers 2365xxxx or 53152001 to perform an action and if none exists, perform another action.

consulta2 = "SELECT * FROM TCMOVIMIENTOCONTABLE WHERE CM1_TERCERO LIKE '%" + TBNit.Text + "' AND CM1_TIPO = '" + comboBoxTipo.Text + "' AND CM1_NUM LIKE '%" + TBCausacion.Text + "'";
            if (lector.Read())
            {
                cuenta1 = lector["CM1_CUENTA"].ToString();
                //textBox4.Text = cuenta1;
                if (int.Parse(cuenta1) == 2365)
                {
                    //MessageBox.Show("Existe 1");
                    TBRete.Text = lector["CM1_VALORH"].ToString();
                }
                else if (int.Parse(cuenta1) == 53152001)
                {
                    //MessageBox.Show("Existe 2");
                    TBRetenAsum.Text = lector["CM1_VALORD"].ToString();
                }
                else
                {
                    TBTotal1.Text = lector["CM1_VALORH"].ToString();
                }
            }

my requirement would be like bringing a variable "account1" if it finds the 2365xxx or 53152001 to perform the action and if it is not in any account simply save a value in a textbox. the value 2365 normally comes "23659999" so the first 4 numbers are what I need in the case of 2365

    
asked by Saul Salazar 10.08.2018 в 22:39
source

3 answers

0

As understood, what you need is to go through all the queries, so according to what you read, it should look like this:

consulta2 = "SELECT * FROM TCMOVIMIENTOCONTABLE WHERE CM1_TERCERO LIKE '%" + TBNit.Text 
    + "' AND CM1_TIPO = '" + comboBoxTipo.Text + "' AND CM1_NUM LIKE '%" + TBCausacion.Text + "'";
    //como dices que la consulta te trae varias filas de resultados, entonces, recorremos la consultas fila por fila
    // y en cada fila comparamos si se cumple cada condicion
    while (lector.Read())
    {
        cuenta1 = lector["CM1_CUENTA"].ToString();
        //textBox4.Text = cuenta1;
        if (cuenta1.StartsWith("2365"))
        {
            //MessageBox.Show("Existe 1");
            TBRete.Text = lector["CM1_VALORH"].ToString();
            break //este es opcional por si solo quieres que termine el ciclo cuando encuentres 2365
        }
        else if (cuenta1.StartsWith("53152001"))
        {
            //MessageBox.Show("Existe 2");
            TBRetenAsum.Text = lector["CM1_VALORD"].ToString();
        }
        else
        {
            TBTotal1.Text = lector["CM1_VALORH"].ToString();
        }
    }

I hope this serves you bro xD ... ReNiceCode ...

    
answered by 10.08.2018 / 23:43
source
0

You must pass it to string and ask if that field starts with 2365, with StartsWith

 if (cuenta1.StartsWith("2365"))
    
answered by 10.08.2018 в 22:49
0

Since you grab it as a String you just have to do this to your code

What I understand is that CM1_CUENTA contains several values what you can do is something like this:

consulta2 = "SELECT * FROM TCMOVIMIENTOCONTABLE WHERE CM1_TERCERO LIKE '%" + TBNit.Text + "' AND CM1_TIPO = '" + comboBoxTipo.Text + "' AND CM1_NUM LIKE '%" + TBCausacion.Text + "'";
    if (lector.Read())
    {
        cuenta1 = lector["CM1_CUENTA"].ToString();
        //textBox4.Text = cuenta1;
        if (cuenta1.Contains("2365")) //Cambio
        {
            int emp = cuenta1.IndexOf("2365", 0) + 4;
            int ter = cuenta1.IndexOf(" ", emp);
            TBRete.Text = cuenta1.Substring(emp, ter - emp);
        }
        else if (cuenta1.Contains("53152001")) //Cambio
        {
            //MessageBox.Show("Existe 2");
            int emp = cuenta1.IndexOf("53152001", 0) + 8;
            int ter = cuenta1.IndexOf(" ", emp);
            TBRetenAsum.Text = cuenta1.Substring(emp, ter - emp);
        }
        else
        {
            TBTotal1.Text = lector["CM1_VALORH"].ToString();
        }
    }
    
answered by 10.08.2018 в 23:07