2017-07-07 2 views
0

In Ordnung, also, das ist ein Code, den ich aus dem Internet bekam, weil ich ziemlich neu in SQL bin und keine Absichten hatte Ich benutze es, bis ich auf diese Login-Funktion stieß, die ich in meine Desktop-Anwendung einfügen wollte. Hier ist der Code.(Gelöst!) Probleme mit einer Schaltfläche nicht funktioniert, wenn SQL verwendet wird, um Daten aus einer Tabelle

SqlConnection connection = new SqlConnection(@"Data Source=JOASH;Initial Catalog=Logins;Integrated Security=True"); 
     SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT COUNT(*) FROM login WHERE Username='" + flatTextBox1.Text + "' AND Password='" + flatTextBox2 + "'", connection); 
     DataTable dataTable = new DataTable(); 
     dataAdapter.Fill(dataTable); 
     if (dataTable.Rows[0][0].ToString() == "1") 
     { 
      this.Hide(); 
      Dashboard dashboard_form = new Dashboard(); 
      dashboard_form.Show(); 
     } 
     else 
     { 
      MessageBox.Show("Authentication failed!"); 
     } 

Das ist der Teil, wo die SQL-Sachen für den Button stattfinden. Grundsätzlich wird beim Anklicken einer Schaltfläche der Code ausgeführt. Aber aus irgendeinem Grund, wenn ich auf die Schaltfläche klicke, wird entweder der Code nicht ausgeführt oder er funktioniert nicht. (Ich schätze, es wird nicht ausgeführt.) Ich hoffe wirklich, dass mir jemand dabei helfen kann. Der Button wird einfach nicht funktionieren und ich habe versucht, es für ein paar Tage zu beheben.

Vielen Dank im Voraus!

BEARBEITEN: Wie von Nirman gefordert, hier ist der Formular-Designer-Code.

namespace subTerraApplication 
    { 
partial class WelcomePage 
{ 
    /// <summary> 
    /// Required designer variable. 
    /// </summary> 
    private System.ComponentModel.IContainer components = null; 

    /// <summary> 
    /// Clean up any resources being used. 
    /// </summary> 
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
    protected override void Dispose(bool disposing) 
    { 
     if (disposing && (components != null)) 
     { 
      components.Dispose(); 
     } 
     base.Dispose(disposing); 
    } 

    #region Windows Form Designer generated code 

    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor. 
    /// </summary> 
    private void InitializeComponent() 
    { 
     this.formSkin1 = new FlatUI.FormSkin(); 
     this.flatTextBox2 = new FlatUI.FlatTextBox(); 
     this.flatLabel2 = new FlatUI.FlatLabel(); 
     this.flatTextBox1 = new FlatUI.FlatTextBox(); 
     this.flatLabel1 = new FlatUI.FlatLabel(); 
     this.flatMini1 = new FlatUI.FlatMini(); 
     this.flatMax1 = new FlatUI.FlatMax(); 
     this.flatClose1 = new FlatUI.FlatClose(); 
     this.flatButton1 = new FlatUI.FlatButton(); 
     this.formSkin1.SuspendLayout(); 
     this.SuspendLayout(); 
     // 
     // formSkin1 
     // 
     this.formSkin1.BackColor = System.Drawing.Color.White; 
     this.formSkin1.BaseColor = System.Drawing.Color.FromArgb(((int)(((byte)(60)))), ((int)(((byte)(70)))), ((int)(((byte)(73))))); 
     this.formSkin1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(53)))), ((int)(((byte)(58)))), ((int)(((byte)(60))))); 
     this.formSkin1.Controls.Add(this.flatButton1); 
     this.formSkin1.Controls.Add(this.flatTextBox2); 
     this.formSkin1.Controls.Add(this.flatLabel2); 
     this.formSkin1.Controls.Add(this.flatTextBox1); 
     this.formSkin1.Controls.Add(this.flatLabel1); 
     this.formSkin1.Controls.Add(this.flatMini1); 
     this.formSkin1.Controls.Add(this.flatMax1); 
     this.formSkin1.Controls.Add(this.flatClose1); 
     this.formSkin1.Dock = System.Windows.Forms.DockStyle.Fill; 
     this.formSkin1.FlatColor = System.Drawing.Color.FromArgb(((int)(((byte)(52)))), ((int)(((byte)(152)))), ((int)(((byte)(219))))); 
     this.formSkin1.Font = new System.Drawing.Font("Segoe UI", 12F); 
     this.formSkin1.HeaderColor = System.Drawing.Color.FromArgb(((int)(((byte)(45)))), ((int)(((byte)(47)))), ((int)(((byte)(49))))); 
     this.formSkin1.HeaderMaximize = false; 
     this.formSkin1.Location = new System.Drawing.Point(0, 0); 
     this.formSkin1.Name = "formSkin1"; 
     this.formSkin1.Size = new System.Drawing.Size(256, 349); 
     this.formSkin1.TabIndex = 0; 
     this.formSkin1.Text = "subTerra Build 1"; 
     // 
     // flatTextBox2 
     // 
     this.flatTextBox2.BackColor = System.Drawing.Color.Transparent; 
     this.flatTextBox2.FocusOnHover = false; 
     this.flatTextBox2.Location = new System.Drawing.Point(125, 135); 
     this.flatTextBox2.MaxLength = 32767; 
     this.flatTextBox2.Multiline = false; 
     this.flatTextBox2.Name = "flatTextBox2"; 
     this.flatTextBox2.ReadOnly = false; 
     this.flatTextBox2.Size = new System.Drawing.Size(109, 29); 
     this.flatTextBox2.TabIndex = 10; 
     this.flatTextBox2.TextAlign = System.Windows.Forms.HorizontalAlignment.Left; 
     this.flatTextBox2.TextColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(192)))), ((int)(((byte)(192))))); 
     this.flatTextBox2.UseSystemPasswordChar = true; 
     // 
     // flatLabel2 
     // 
     this.flatLabel2.AutoSize = true; 
     this.flatLabel2.BackColor = System.Drawing.Color.Transparent; 
     this.flatLabel2.Font = new System.Drawing.Font("Segoe UI", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 
     this.flatLabel2.ForeColor = System.Drawing.Color.White; 
     this.flatLabel2.Location = new System.Drawing.Point(13, 135); 
     this.flatLabel2.Name = "flatLabel2"; 
     this.flatLabel2.Size = new System.Drawing.Size(99, 30); 
     this.flatLabel2.TabIndex = 9; 
     this.flatLabel2.Text = "Password"; 
     // 
     // flatTextBox1 
     // 
     this.flatTextBox1.BackColor = System.Drawing.Color.Transparent; 
     this.flatTextBox1.FocusOnHover = false; 
     this.flatTextBox1.Location = new System.Drawing.Point(125, 81); 
     this.flatTextBox1.MaxLength = 32767; 
     this.flatTextBox1.Multiline = false; 
     this.flatTextBox1.Name = "flatTextBox1"; 
     this.flatTextBox1.ReadOnly = false; 
     this.flatTextBox1.Size = new System.Drawing.Size(109, 29); 
     this.flatTextBox1.TabIndex = 8; 
     this.flatTextBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Left; 
     this.flatTextBox1.TextColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(192)))), ((int)(((byte)(192))))); 
     this.flatTextBox1.UseSystemPasswordChar = false; 
     // 
     // flatLabel1 
     // 
     this.flatLabel1.AutoSize = true; 
     this.flatLabel1.BackColor = System.Drawing.Color.Transparent; 
     this.flatLabel1.Font = new System.Drawing.Font("Segoe UI", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 
     this.flatLabel1.ForeColor = System.Drawing.Color.White; 
     this.flatLabel1.Location = new System.Drawing.Point(13, 81); 
     this.flatLabel1.Name = "flatLabel1"; 
     this.flatLabel1.Size = new System.Drawing.Size(106, 30); 
     this.flatLabel1.TabIndex = 7; 
     this.flatLabel1.Text = "Username"; 
     // 
     // flatMini1 
     // 
     this.flatMini1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); 
     this.flatMini1.BackColor = System.Drawing.Color.White; 
     this.flatMini1.BaseColor = System.Drawing.Color.FromArgb(((int)(((byte)(45)))), ((int)(((byte)(47)))), ((int)(((byte)(49))))); 
     this.flatMini1.Font = new System.Drawing.Font("Marlett", 12F); 
     this.flatMini1.Location = new System.Drawing.Point(189, -1); 
     this.flatMini1.Name = "flatMini1"; 
     this.flatMini1.Size = new System.Drawing.Size(18, 18); 
     this.flatMini1.TabIndex = 6; 
     this.flatMini1.Text = "flatMini1"; 
     this.flatMini1.TextColor = System.Drawing.Color.FromArgb(((int)(((byte)(243)))), ((int)(((byte)(243)))), ((int)(((byte)(243))))); 
     // 
     // flatMax1 
     // 
     this.flatMax1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); 
     this.flatMax1.BackColor = System.Drawing.Color.White; 
     this.flatMax1.BaseColor = System.Drawing.Color.FromArgb(((int)(((byte)(45)))), ((int)(((byte)(47)))), ((int)(((byte)(49))))); 
     this.flatMax1.Font = new System.Drawing.Font("Marlett", 12F); 
     this.flatMax1.Location = new System.Drawing.Point(213, -1); 
     this.flatMax1.Name = "flatMax1"; 
     this.flatMax1.Size = new System.Drawing.Size(18, 18); 
     this.flatMax1.TabIndex = 5; 
     this.flatMax1.Text = "flatMax1"; 
     this.flatMax1.TextColor = System.Drawing.Color.FromArgb(((int)(((byte)(243)))), ((int)(((byte)(243)))), ((int)(((byte)(243))))); 
     // 
     // flatClose1 
     // 
     this.flatClose1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); 
     this.flatClose1.BackColor = System.Drawing.Color.White; 
     this.flatClose1.BaseColor = System.Drawing.Color.FromArgb(((int)(((byte)(168)))), ((int)(((byte)(35)))), ((int)(((byte)(35))))); 
     this.flatClose1.Font = new System.Drawing.Font("Marlett", 10F); 
     this.flatClose1.Location = new System.Drawing.Point(237, 0); 
     this.flatClose1.Name = "flatClose1"; 
     this.flatClose1.Size = new System.Drawing.Size(18, 18); 
     this.flatClose1.TabIndex = 4; 
     this.flatClose1.Text = "flatClose1"; 
     this.flatClose1.TextColor = System.Drawing.Color.FromArgb(((int)(((byte)(243)))), ((int)(((byte)(243)))), ((int)(((byte)(243))))); 
     // 
     // flatButton1 
     // 
     this.flatButton1.BackColor = System.Drawing.Color.Transparent; 
     this.flatButton1.BaseColor = System.Drawing.Color.FromArgb(((int)(((byte)(52)))), ((int)(((byte)(152)))), ((int)(((byte)(219))))); 
     this.flatButton1.Cursor = System.Windows.Forms.Cursors.Hand; 
     this.flatButton1.Font = new System.Drawing.Font("Segoe UI", 12F); 
     this.flatButton1.Location = new System.Drawing.Point(74, 209); 
     this.flatButton1.Name = "flatButton1"; 
     this.flatButton1.Rounded = false; 
     this.flatButton1.Size = new System.Drawing.Size(106, 32); 
     this.flatButton1.TabIndex = 11; 
     this.flatButton1.Text = "Login"; 
     this.flatButton1.TextColor = System.Drawing.Color.FromArgb(((int)(((byte)(243)))), ((int)(((byte)(243)))), ((int)(((byte)(243))))); 
     // 
     // WelcomePage 
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.ClientSize = new System.Drawing.Size(256, 349); 
     this.Controls.Add(this.formSkin1); 
     this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; 
     this.Name = "WelcomePage"; 
     this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 
     this.Text = "Form1"; 
     this.TransparencyKey = System.Drawing.Color.Fuchsia; 
     this.formSkin1.ResumeLayout(false); 
     this.formSkin1.PerformLayout(); 
     this.ResumeLayout(false); 

    } 

    #endregion 

    private FlatUI.FormSkin formSkin1; 
    private FlatUI.FlatMini flatMini1; 
    private FlatUI.FlatMax flatMax1; 
    private FlatUI.FlatClose flatClose1; 
    private FlatUI.FlatTextBox flatTextBox2; 
    private FlatUI.FlatLabel flatLabel2; 
    private FlatUI.FlatTextBox flatTextBox1; 
    private FlatUI.FlatLabel flatLabel1; 
    private FlatUI.FlatButton flatButton1; 
} 

}

EDIT 2: Problem gelöst ist! Der Code für die Klickerkennung fehlt! Vielen Dank für die Hilfe.

+0

platzieren Sie einen Haltepunkt auf Ihre Schaltfläche klicken Ereignis in der Code, und stellen Sie sicher, ob es wirklich ausgeführt wird oder nicht. Wenn nicht, müssen Sie Ihren Formular-Designer-Code hier einfügen. – Nirman

+0

Es wird nicht ausgeführt. Lassen Sie mich den Formular-Designer-Code aufspüren. – JamesPlays

+0

Formular Designercode hinzugefügt. – JamesPlays

Antwort

1

Ich sehe keine an den Designer-Code der Taste angebracht Ereignisbehandlungsroutine sollte es sein:

this.flatButton1.Click += new System.EventHandler(this.flatButton1_Click); 

Sie müssen definieren ein Event-Handler und setzen Sie den Code darin

private void flatButton1_Click(object sender, EventArgs e) 
    { 
     //Your code here 
    } 
+0

Ich habe nur nicht die private angezeigt Teil im Code ungültig, es ist sehr präsent. – JamesPlays

0

Haben Sie versucht, den Haltepunkt in if/else zu setzen?

Und hier einige Überlegungen: Verwenden Sie vorbereitete Anweisungen und parametrisierte Abfragen. Erstellen Sie niemals dynamisches SQL basierend auf Benutzereingaben, da die Anwendung anfällig für SQL injection wird.

Hier wird Sie Code unter Verwendung parametrisierte Parameter neu geschrieben:

SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT COUNT(*) FROM login WHERE [email protected] AND [email protected]", connection); 
dataAdapter.SelectCommand.Parameters.AddWithValue("@Username", flatTextBox1.Text); 
dataAdapter.SelectCommand.Parameters.AddWithValue("@Password", flatTextBox2.Text); 
Verwandte Themen