簡體   English   中英

如果在TextBox_TextChanged中不相等,則無法正常工作

[英]if not equal in TextBox_TextChanged not working

TextBox2輸入名稱時,它將檢查該名稱是否在數據庫中。 如果是密碼,則出現文本框。

該代碼將一直運行到輸入名稱匹配為止。 問題是刪除一個字母會使密碼文本框消失。

然而,一旦找到匹配的名稱,密碼文本框將始終可見:

private void TextBox2_TextChanged(object sender, EventArgs e)
{
    string UN = TextBox2.Text;
    string connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\FixITAdmin.mdb";
    string queryString = "SELECT Admins.Name AS [Admins Name] FROM Admins AS Admins WHERE Admins.Name ='" + TextBox2.Text + "' ORDER BY  Admins.Name";

    try
    {
        using (OleDbConnection connection = new OleDbConnection(connString))
        {
            OleDbCommand command = new OleDbCommand(queryString, connection);
            connection.Open();
            OleDbDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                string UName = reader.GetValue(0).ToString();
                if (UName == UN)
                {
                    Pass_textBox.Visible = true;
                    Pass_textBox.Enabled = true;
                    SP_checkBox.Visible = true;
                    SP_checkBox.Enabled = true;
                    SP_label.Visible = true;
                    SP_label.Enabled = true;

                }
                else if (UName != UN)
                {
                    Pass_textBox.Visible = false;
                    Pass_textBox.Enabled = false;
                    SP_checkBox.Visible = false;
                    SP_checkBox.Enabled = false;
                    SP_label.Visible = false;
                    SP_label.Enabled = false;

                }
            }

            reader.Close();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

謝謝你的指導

我現在將代碼更改為

try
        {
            using (OleDbConnection connection = new OleDbConnection(connString))
            {
                OleDbCommand command = new OleDbCommand(queryString, connection);
                connection.Open();
                OleDbDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    string UName = reader.GetValue(0).ToString();
                    if (UName == UN)
                    {
                        Pass_textBox.Visible = true;
                        Pass_textBox.Enabled = true;
                        SP_checkBox.Visible = true;
                        SP_checkBox.Enabled = true;
                        SP_label.Visible = true;
                        SP_label.Enabled = true;
                        found = 1;                      
                    }

                }
                reader.Close();
            }
            if (found != 1)
            {
                Pass_textBox.Visible = false;
                Pass_textBox.Enabled = false;
                SP_checkBox.Visible = false;
                SP_checkBox.Enabled = false;
                SP_label.Visible = false;
                SP_label.Enabled = false;

            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM