如果在TextBox_TextChanged中不相等,则无法正常工作

时间:2019-07-06 19:01:03

标签: c#

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());
    }
}

1 个答案:

答案 0 :(得分:0)

感谢指针

我现在将代码更改为

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());
        }