ELSE语句不起作用

时间:2019-07-09 11:08:23

标签: c#

代码将查看文本框,并将其与数据库中的值进行比较(如果值匹配),则会显示一个消息框,表明该框已存在并禁用保存按钮。这部分工作正常,但是当我将文本框中的文本更改为与数据库中的值不匹配的内容时,应该启用“保存”按钮,但事实并非如此。实际上,我必须先关闭表单并重新打开它,然后才能再次启用保存按钮。

private void Name_textBox_TextChanged(object sender, EventArgs e)
{
    string UN = Name_textBox.Text;

    string connString = @"Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\Users\Paul\source\repos\FixIT\FixIT\bin\Debug\FixITAdmin.mdb";
    string queryString = "SELECT Admins.Name AS [Admins Name] FROM Admins AS Admins WHERE Admins.Name ='" + Name_textBox.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)
                {

                    Add_button.Enabled = false;

                }
                else
                if (UName != UN)
                {
                    Add_button.Enabled = true;
                }
            }
            reader.Close();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

0 个答案:

没有答案