2 Replies - 150 Views - Last Post: 09 July 2019 - 07:32 PM Rate Topic: -----

#1 pds8475   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 20-April 17

else not working in IF Else statement

Posted 09 July 2019 - 11:21 AM

Hi
I'm new to programming plus I have had a stroke. So please be patient.
I have a form with a textbox(Name_textbox) as well as a button(Add_button)
I also have an access database file(FixITAdmin) with a table(Admins) which has a field(Name)

I am using the TextChanged event of the textbox to check whether the text the user has entered exists in the database.
If it does exist a messagebox appears telling the user and disabling the button.

That part works fine.

My problem is I want it enable the button when the user alters the text, but it doesn't.
Instead I have to exit and go back into the form before the button is enabled.

It appears from stepping through the code that it never looks at the Else part of the statement.
However why it does this I don't know.

 private void Name_textBox_TextChanged(object sender, EventArgs e)
        {
            string UN = Name_textBox.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 ='" + 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();

                        bool status = UN.Equals(UName);
                        if (status)
                        {
                           
                            this.Add_button.Enabled = false;
                            MessageBox.Show("Name already exist in the Database");
                        }
                        else
                        {
                            this.Add_button.Enabled = true;
                        }

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




Is This A Good Question/Topic? 0
  • +

Replies To: else not working in IF Else statement

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15113
  • View blog
  • Posts: 60,464
  • Joined: 12-June 08

Re: else not working in IF Else statement

Posted 09 July 2019 - 11:40 AM

Quote

I am using the TextChanged event of the textbox to check whether the text the user has entered exists in the database.

You probably do not want to do that. Text change could fire from a single letter then mis click elsewhere, switched focus, or tab out. Better to have a discrete action like a button.

Better yet the 'add' button could call a _VALIDATE_ function that checks if the name is in the db or not and continues on if there or not.

BUTTON CLICK START
  IF not doesExist(<some string>)
    DO add thing
  END IF
BUTTON CLICK STOP

Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6968
  • View blog
  • Posts: 23,684
  • Joined: 05-May 12

Re: else not working in IF Else statement

Posted 09 July 2019 - 07:32 PM

Just playing Devil's Advocate: Adding that "Add" button is not very Web 2.0-like. A lot of modern UI now doesn't seem to have any explicit "Save", "OK", "Submit", or "Apply" buttons anymore. They just seem to apply the changes directly while manipulating the UI.

Back to my normal self: Personally, I hate a lot of the current Web 2.0 UI paradigms like the missing explicit acceptance buttons. I say that if our OP is using as antiquated as WinForms, it behooves them to follow the UI paradigms/guidelines of that era. So let there be an "Add" button which should be correct time to try to validate the inputs.

Actually, since our OP is using WinForms, so in for a penny, in for a pound. He should make use of the built in WinForms ErrorProvider and validation.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1