Hello,

I've enountered a problem whith saving to an XML file. My app receives user input in several text boxes and when the user hits the OK button it checks if the text boxes are empty. If they are not empty it saves the input to an XML file and closes the window, but if they aren't, it returns a message box with explanations on which text box is empty. Every text box si checked individually.

If a text box is empty the message box is opened as intended, but when confirming the message box it continues to save the input to the XML file and because it is empty and it has nothing to save it returns an error. I want it to stop executing the code when it finds an empty text box, display the message and simply return to the window to allow the user to enter the data.

Source code bellow. (just the start... as it continues the same for each text box and save)

private void btnOK_Click(object sender, EventArgs e)
        {
            checkIfEmpty();

            XmlDocument maindoc = new XmlDocument();
            maindoc.Load(@"C:\\Windows\Temp\SessionsTotal.xml");

            foreach (XmlNode node in maindoc.SelectNodes("//X1"))
            {
                node.InnerText = (XmlConvert.ToDouble(node.InnerText) + XmlConvert.ToDouble(X1.Text)).ToString();
            }
            foreach (XmlNode node in maindoc.SelectNodes("//X2"))
            {
                node.InnerText = (XmlConvert.ToDouble(node.InnerText) + XmlConvert.ToDouble(X2.Text)).ToString();
            }
public void checkIfEmpty()
        {
            if (X1.Text == String.Empty)
            {
                MessageBox.Show("The number of Products must be entered", "Products number not entered", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (X2.Text == String.Empty)
            {
                MessageBox.Show("The Surface Area must be entered", "Surface Area not entered", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (X3.Text == String.Empty)
            {
                MessageBox.Show("The number of Persons must be entered", "Persons number not entered", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

Thank you.

Hello,

I've enountered a problem whith saving to an XML file. My app receives user input in several text boxes and when the user hits the OK button it checks if the text boxes are empty. If they are not empty it saves the input to an XML file and closes the window, but if they aren't, it returns a message box with explanations on which text box is empty. Every text box si checked individually.

If a text box is empty the message box is opened as intended, but when confirming the message box it continues to save the input to the XML file and because it is empty and it has nothing to save it returns an error. I want it to stop executing the code when it finds an empty text box, display the message and simply return to the window to allow the user to enter the data.

Source code bellow. (just the start... as it continues the same for each text box and save)

private void btnOK_Click(object sender, EventArgs e)
        {
            checkIfEmpty();

            XmlDocument maindoc = new XmlDocument();
            maindoc.Load(@"C:\\Windows\Temp\SessionsTotal.xml");

            foreach (XmlNode node in maindoc.SelectNodes("//X1"))
            {
                node.InnerText = (XmlConvert.ToDouble(node.InnerText) + XmlConvert.ToDouble(X1.Text)).ToString();
            }
            foreach (XmlNode node in maindoc.SelectNodes("//X2"))
            {
                node.InnerText = (XmlConvert.ToDouble(node.InnerText) + XmlConvert.ToDouble(X2.Text)).ToString();
            }
public void checkIfEmpty()
        {
            if (X1.Text == String.Empty)
            {
                MessageBox.Show("The number of Products must be entered", "Products number not entered", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (X2.Text == String.Empty)
            {
                MessageBox.Show("The Surface Area must be entered", "Surface Area not entered", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (X3.Text == String.Empty)
            {
                MessageBox.Show("The number of Persons must be entered", "Persons number not entered", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

Thank you.

Add a boolean variable and set it to some default value say "True" and if there is any empty set the value to "False" and check for the boolean value inside the button click event before the XML code starts. If True execute the XML codes or else do nothing. Hope this solves.

bool bCheck = true;

private void btnOK_Click(object sender, EventArgs e)
        {
            checkIfEmpty();

            if(bCheck)
            {
                 XmlDocument maindoc = new XmlDocument();
                 maindoc.Load(@"C:\\Windows\Temp\SessionsTotal.xml");
            }
        }
public void checkIfEmpty()
        {
            if (X1.Text == String.Empty)
            {
                MessageBox.Show("The number of Products must be entered", "Products number not entered", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                bCheck = false;
            }
            else if (X2.Text == String.Empty)
            {
                MessageBox.Show("The Surface Area must be entered", "Surface Area not entered", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                bCheck = false;
            }
            else if (X3.Text == String.Empty)
            {
                MessageBox.Show("The number of Persons must be entered", "Persons number not entered", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                bCheck = false;
            }

Whoa... that's a really gross way to do it, though... You don't want to introduce a global variable like that. Just have the checkIfEmpty() method return a bool value.

try this checkIfEmpty method:

public bool checkIfEmpty()
        {
            if (X1.Text == String.Empty)
            {
                MessageBox.Show("The number of Products must be entered", "Products number not entered", MessageBoxButtons.OK, MessageBoxIcon.Warning);
		return false;
            }
            else if (X2.Text == String.Empty)
            {
                MessageBox.Show("The Surface Area must be entered", "Surface Area not entered", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            else if (X3.Text == String.Empty)
            {
                MessageBox.Show("The number of Persons must be entered", "Persons number not entered", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

	    return true;
	}

And then you could use it like this:

private void btnOK_Click(object sender, EventArgs e)
        {
            checkIfEmpty();
 
            if(checkIfEmpty())
            {
                 XmlDocument maindoc = new XmlDocument();
                 maindoc.Load(@"C:\\Windows\Temp\SessionsTotal.xml");
            }
        }

Check out this resource to see why global variables should be avoided:

http://c2.com/cgi/wiki?GlobalVariablesAreBad

Edited 6 Years Ago by alc6379: additional information link added.

This question has already been answered. Start a new discussion instead.