Frage

Good day sir! I able to call a data from my database using reader and after getting a data in the database their will be a messageBox appear when I get my expected data. Here's my sample code:

if (textBox5.Text == "")
        { }
        else
        {
            DialogResult ms = MessageBox.Show("Try Again", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            if (ms == DialogResult.OK)
            {
                textBox5.Clear();
                textBox1.Clear();
                listBox5.Items.Clear();
                textBox3.Clear();
                listBox4.Items.Clear();
            }

        }

        while (reader.Read())
        {
            listBox4.Items.Clear();
            if (string.Compare(label1.Text, reader.GetString(0)) == 0)
            {
                for (int t = 1; t < 11; t++)
                {
                    words.Add(reader.GetString(t));
                }
            }
            words.Shuffle();
            listBox4.Items.AddRange(words.ToArray());
        }
        reader.Close();
        coon.Close();

My problem here is when I apply these codes, there are two messageBox will appear so I remove this codes :

                textBox5.Clear();
                textBox1.Clear();
                listBox5.Items.Clear();
                textBox3.Clear();
                listBox4.Items.Clear();

all I want is I want to show the messageBox once. Can u help me with this?

War es hilfreich?

Lösung

You have not shown enough code, but I'm going to assume this function is inside a TextChanged event.

If that is the case, you're checking if TextBox5 has some text, then you're clearing a number of controls on the form. You haven't shown other events and what they do, so it would be hard to tell you more at this point.

Consider debugging your code and stepping through it line by line, that way you can follow the logic of the program flow and see where the hiccup occurs...

I'd also change the first 3 lines:

if (textBox5.Text == "")
    { }
    else

with if (!string.IsNullOrEmpty(this.textBox5.Text.Trim())), which means, if string is not empty (null or empty to be precise).

Hope this helps.
Good luck

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top