2016-05-18 11 views
0

Ich habe ein Login-Formular mit Zugriff db gemacht, von dem ich nur das Passwort brauche. Die Verbindung zu db ist erfolgreich, aber wenn ich den Knopf drücke, zeigt mir die Nachrichtenbox ein "falsches Passwort" an, auch wenn ich ein korrektes aus meiner db einfüge. hier ist mein Code:C# Login-Formular funktioniert nicht

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Data.OleDb; 

namespace TAC_receptie 
{ 
    public partial class login : Form 
    { 

    OleDbConnection connection = new OleDbConnection(); 
    public login() 
    { 
     InitializeComponent(); 
     connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:login1.accdb;Persist Security Info=False;"; 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     connection.Open(); 
     OleDbCommand command = new OleDbCommand(); 
     command.Connection = connection; 
     command.CommandText="select * from user1 where Password =' " +txtPassword.Text+ " '"; 
     OleDbDataReader reader = command.ExecuteReader(); 
     int count = 0; 
     while (reader.Read()) 
     { 
      count++; 
     } 

     if (count == 1) 
     { 
      date_personale Form = new date_personale(); 
      Form.Show(); 
     } 
     else 
     { 
      if (count > 1) 
      { 
       MessageBox.Show("duplicat!!!"); 
      } 

      if (count < 1) 
      { 
       MessageBox.Show("parola incorecta!!!"); 
      } 
     } 

     connection.Close(); 
    } 

    private void login_Load(object sender, EventArgs e) 
    { 
     try 
     {   
      connection.Open(); 

      connection.Close(); 
     } 
     catch(Exception ex) 
     { 
      MessageBox.Show("Error"+ ex); 
     } 
    } 

    private void txtPassword_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     if (e.KeyChar == (char)13) 
      btnLogin.PerformClick(); 
    } 

} 

}

Antwort

4

Entfernen unnötiger Raum von

Password =' " +txtPassword.Text+ " '" 

zu

Password ='" +txtPassword.Text.Trim()+ "'" 
+0

wow. es läuft jetzt gut. Danke! –