Login screen with C#database!

Asked

Viewed 4,056 times

1

Eae, guys! So... I’m pretty new to programming and I’m having some problems with the database login screen. The code does not give any error, but it is as if there was no user registration in 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 value variable does not change its value. I wonder if you could 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();


        }

    }

Thank you if you can help!!

1 answer

1


There’s nothing wrong with the code. You should check the table "user", the columns(fields) name, password and email can contain spaces, and upper and lower case influence the search.

Use Microsoft SQL Server Management Studio to test 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

  • Damn, man! Thank you so much! Somehow the other form I had created to register the user was adding space before the name, password and email because of my code. I’ve already arranged it here, I can’t believe I stayed 2 days on it, hahaha. Thank you very much, confrere!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.