Validate registration and compare it with textbox, someone who can help me! Query1 is the same as TextBox1.Text but it does not validate it sends me else [closed]

0
protected void TextBox1_TextChanged(object sender, EventArgs e)
{


    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCafeteriaConnectionString"].ConnectionString);

    con.Open();
    string Query1 = "select Id from Tbl_Empleados where Id='" + TextBox1.Text + "'";
    string query2 = "select Id from Tbl_Registros where Id='" + TextBox1.Text + "'";
    string insert = "insert into Tbl_Registros (Id,Num_Empleado,Nombre,Apellido,Fecha_Hora) values (@Id,@Num_Empleado,@Nombre,@Apellido,@Fecha_Hora)";

    if (string.IsNullOrEmpty(TextBox1.Text))
    {
        Lbl_Mensaje.Text = ("Error");
    }
    else
    {

        SqlCommand cmd1 = new SqlCommand(Query1, con);
        cmd1.Parameters.AddWithValue("@Id", TextBox1.Text);
        cmd1.ExecuteNonQuery();


        if (Query1 == TextBox1.Text)
        {
            Lbl_Mensaje.Text = ("paso query 1!!!");
            SqlCommand cmd2 = new SqlCommand(query2, con);
            cmd2.Parameters.AddWithValue("@Id", TextBox1.Text);
            cmd2.ExecuteNonQuery();

            if (query2 == TextBox1.Text)
            {
                Lbl_Mensaje.Text = ("Empleado ya registrado el dia de hoy!!!");

                con.Close();
            }
            else
            {

                SqlCommand cmd = new SqlCommand(insert, con);
                cmd.Parameters.AddWithValue("@Id", TextBox1.Text);
                cmd.Parameters.AddWithValue("@Num_Empleado", TextBox1.Text);
                cmd.Parameters.AddWithValue("@Nombre", TextBox1.Text);
                cmd.Parameters.AddWithValue("@Apellido", TextBox1.Text);
                cmd.Parameters.AddWithValue("@Fecha_Hora", DateTime.Now);
                cmd.ExecuteNonQuery();
                con.Close();
                Lbl_Mensaje.Text = ("Empleado  registrado correctamente!!!");

            }

        }
        else
        {
            Lbl_Mensaje.Text = ("Empleado no existe!!!");

        }
    }

}
    
asked by klark 07.02.2018 в 23:46
source

1 answer

0

to be able to validate you need the SqlDataReader I show you an example code:

    public bool VerificarEmpleado() // Este metodo te sirve para regresar una valor Boolenao (TRUE O FLASE)
    {
        bool respuesta = false;
        con.Open();
        SqlCommand cmd = new SqlCommand("select Id from Tbl_Empleados where Id='" + TextBox1.Text + "'", con);
        SqlDataReader reader = cmd.ExecuteReader();
        if (reader.Read() == true)
        {
            respuesta = true; //la varibable se establece en TRUE
        }
        con.Close();
        return respuesta; // te regresa el valor 
    }  
    
answered by 09.02.2018 / 23:39
source