How to check user in login screen?

2

I have a problem, I have a login screen and I need to check it with a query Sql .

Get something done, but it does not check and goes straight through.

private void btn_Entrar_Click(object sender, EventArgs e)
    {

        SqlConnection conexao = new SqlConnection();

        conexao.ConnectionString = Properties.Settings.Default.cs1;


        try
        {
            conexao.Open();
            SqlCommand obj = new SqlCommand();

            obj.CommandText = "Select * FROM TB_LOGIN WHERE Usuario =@Usuario And  Senha=@Senha";


            obj.Parameters.Add("@Usuario", SqlDbType.VarChar).Value =  Txt_Nome.Text;
            obj.Parameters.Add("@Senha", SqlDbType.VarChar).Value = Txt_Senha.Text;

            obj.CommandType = CommandType.Text;
            obj.Connection = conexao;
            obj.ExecuteNonQuery();

            conexao.Close(); 


        }
        catch
        {
            MessageBox.Show("Falha ");
            conexao.Close();
        }
    
asked by anonymous 21.09.2016 / 21:26

1 answer

0

Change your method to the below and validate your user

protected void btn_Entrar_Click(object sender, EventArgs e)
        {
            SqlConnection conexao = new SqlConnection();
            conexao.ConnectionString = ConnString;
            conexao.Open();
            SqlCommand obj = new SqlCommand();
            obj.CommandText = "Select * FROM TB_LOGIN WHERE Usuario =@Usuario And  Senha=@Senha";
            obj.Parameters.Add("@Usuario", SqlDbType.VarChar).Value = Txt_Nome.Text;
            obj.Parameters.Add("@Senha", SqlDbType.VarChar).Value = Txt_Senha.Text;
            obj.CommandType = CommandType.Text;
            obj.Connection = conexao;

            SqlDataReader reader = obj.ExecuteReader();
            if (reader.HasRows)
            {
                //todo : usuario logado
                Response.Redirect(Request.QueryString["ReturnUrl"]); // ????
            }
            else
            {
                //todo : Login ou Senha Invalida
            }
            conexao.Close();
        }
    
21.09.2016 / 21:48