Login and password with c#

ahlam1 Posted messages 33 Status Member -  
 karima -
Good evening,
I’m a beginner in C#
I want code that lets me verify if the password and login entered by the user are present in my table (I’m working with Access)
I wrote this code in the login button but it doesn’t work:
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;..........
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "SELECT COUNT(login) FROM utilisateur WHERE login= '" + textBox1.Text + "' AND motdepasse= '" + textBox2.Text + "'";
cmd.Connection = con;
Int32 cin = (Int32)cmd.ExecuteScalar();
if (cin<0)
{
MessageBox.Show("attention :Le mot de passe ou le login est incorrecte");
}
else {
MessageBox.Show("vous êtes connecter");
choixDeService cds = new choixDeService();
cds.Show();

}
con.Close();

if someone can give me the code or correct this one I would be very grateful
thanks in advance
Configuration: Windows 7 / Firefox 3.5.8

9 answers

  1. holow1 Posted messages 739 Status Member 71
     
    Hello,

    there is a small error I think here: if (cin<0) you should write if ( cin =< 0) or if (cin==0)

    because cmd.ExecuteScalar(); returns 0 is there any records and is the number of records is there really any record so your case it will return 1 because there is a single record that matches your criterion

    on the other hand I didn't understand the role of this part of the code can you explain to us

    choixDeService cds = new choixDeService();
    cds.Show();


    Advertisement removed Moderation CCM
    2
  2. ahlam1 Posted messages 33 Status Member 2
     
    Good evening,
    thank you for your answer
    but I tried with ( cin <= 0) and (cin==0) but it doesn’t work
    if you have another idea or another solution ........

    for
    choixDeService cds = new choixDeService();
    cds.Show();
    choixDeService is a form
    I meant if the password and login are correct then open this form

    thanks
    1
  3. holow1 Posted messages 739 Status Member 71
     
    Hello;

    try like this

    OleDbCommand cmd = new OleDbCommand(); 
    cmd.CommandText = "SELECT COUNT(*) FROM utilisateur WHERE login= '" + textBox1.Text + "' AND motdepasse= '" + textBox2.Text + "'";
    cmd.Connection = con;
    con.Open();
    Int32 cin = (Int32)cmd.ExecuteScalar();
    if (cin==0)
    {
    MessageBox.Show("vous êtes connecter");
    choixDeService cds = new choixDeService();
    cds.Show();

    }
    else {
    MessageBox.Show("attention :Le mot de passe ou le login est incorrecte");
    }
    con.Close();


    Publicité supprimée Modération CCM
    0
  4. ahlam1 Posted messages 33 Status Member 2
     
    it didn't work
    what you say allows the user to connect even if the login or password are incorrect
    0
  5. holow1 Posted messages 739 Status Member 71
     
    hello,

    cmd.ExecuteScalar() : returns 0 if there are no records and the number otherwise is that ?

    in your case : it will return 1 if the password and the login are correct and 0 otherwise ?

    so :

    if (cin!=0) 
    {
    MessageBox.Show("you are connected");
    choixDeService cds = new choixDeService();
    cds.Show();

    }
    else {
    MessageBox.Show("warning: The password or login is incorrect");
    }
    con.Close();


    are there errors in your DB ?

    Advertisement removed CCM Moderation
    0
  6. ahlam1 Posted messages 33 Status Member 2
     
    yes of course that there are ERG in the database
    i try but it executes the else even if i write the login and the password correct

    if you have another idea it would be great
    0
  7. holow1 Posted messages 739 Status Member 71
     
    Hello;

    try with a sqldatareader

    SqlDataReader dr = default(SqlDataReader);
    int r = 0;

    cmd = new SqlCommand("select count(*) from pl where login = '" + TextBox1.Text + "' and passe = '" + TextBox2.Text + "'", cnx);
    cnx.Open();
    dr = cmd.ExecuteReader;
    dr.Read();
    r = dr(0);
    dr.Close();
    cnx.Close();

    if (r != 0)
    {
    MessageBox.Show("you are connected");
    }
    else {

    MessageBox.Show("warning: The password or login is incorrect");
    }
    }
    0
  8. ahlam1 Posted messages 33 Status Member 2
     
    (moi je travail sur access et n'est pas sur sqlsever j changer votre prog avec les donnees accepter par access comme SqlDataReader par oledatareader) mais
    dr est une variable ce n'est pas une methode on peux pas mettre dr(0)
    il ne la pas accepte
    0
  9. karima
     
    OleDbDataAdapter da = new OleDbDataAdapter("select * from Users where Login='" + tlogin.Text + "' and Password='" + tpassword.Text + "'", Program.con);
    DataSet ds = new DataSet();
    da.Fill(ds);
    if (ds.Tables[0].Rows.Count != 0)
    {
    MessageBox.Show("Bienvenu ", "Bienvenue", MessageBoxButtons.OK, MessageBoxIcon.Information);
    GPDR09 f = new GPDR09();
    f.Show();
    this.Hide();

    }
    else
    MessageBox.Show("Votre Nom ou Password est incorect");
    tlogin.Clear();
    tpassword.Clear();
    0