Login Screen with C # Database!

1

Hey, everyone! So ... I'm really new to programming and I'm having problems with the login screen with database. The code does not give any error, but it is as if there was no user registration in the SQL server, I tried all 5 entries that exist and nothing, I only get the message that the data is incorrect, it seems that the variable value does not change its value. Could you help me?!

public void Logar()
    {


     string con = " server = DESKTOP-6BAVUH0; Database = Cadastro; Trusted_Connection = true";
    SqlConnection Conex = new SqlConnection(con);

    string coman = @"SELECT Count (*) FROM usuario WHERE Login = @nome AND Senha = @password AND [E-mail] = @email" ;

        string nome= textBox1.Text, password = textBox2.Text, email = textBox3.Text ;
        try

        {


            SqlCommand comando = new SqlCommand(coman, Conex);

            comando.Parameters.AddWithValue("@nome",nome);
            comando.Parameters.AddWithValue("@password",password);
            comando.Parameters.AddWithValue("@email", email);
            Conex.Open();

            int valor = (int)comando.ExecuteScalar();
            if (valor > 0)
            {
                Logado = true;
                MessageBox.Show("Logado com sucesso. Sejam bem-vindo!");

                this.Close();
            }
            else
            {
                Logado = false;
                MessageBox.Show("Dados incorretos!");

            }
        }

        catch (SqlException erro)

        {

            MessageBox.Show(erro + "Na conexão com o banco de dados");
        }

        finally
        {
            Conex.Close();


        }

    }

Grateful if you can help !!

    
asked by anonymous 02.11.2015 / 00:42

1 answer

1

The code does not have any problems. You should check the table "user", the columns (fields) name, password and e-mail may contain spaces, and case influences the search.

Use Microsoft SQL Server Management Studio to test your searches, see the example:

SELECT [Login]
      ,[Senha]
      ,[E-Mail]
  FROM [Cadastro].[dbo].[Usuario]
  where [Login] = 'John' and
    Senha = '123456' and
    [E-mail] = '[email protected]'

Antonio

    
02.11.2015 / 02:01