简体   繁体   English

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

[英]if not equal in TextBox_TextChanged not working

When you enter a name in TextBox2 it checks whether that name is in the database. TextBox2输入名称时,它将检查该名称是否在数据库中。 If it is a password textbox appears. 如果是密码,则出现文本框。

The code runs fine up till and including a name matching is entered. 该代码将一直运行到输入名称匹配为止。 The problem is that deleting one letter should make the password textbox disappear. 问题是删除一个字母会使密码文本框消失。

Yet once a matching name has been found the password textbox is always visible: 然而,一旦找到匹配的名称,密码文本框将始终可见:

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

Thanks for the pointer 谢谢你的指导

I have now changed the code to 我现在将代码更改为

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