0

This code works fine except multiple dialog box prompt when there are multiple empty textbox but I only want it to prompt once.

For example, if I enter 1,1,(null),(null),d,g, dialog box will prompt twice since there is two empty textboxes but I only need it to prompt once.

How can I solve this problem?

public void BeforeSave(BCE.AutoCount.Invoicing.Sales.SalesOrder.SalesOrderBeforeSaveEventArgs e)
    {
        for (int i = 0; i < e.MasterRecord.DetailCount; i++)
        {
            if (String.IsNullOrEmpty(e.MasterRecord.GetDetailRecord(i).YourPONo.ToString()))
            {
        MessageBox.Show("You left Your PO No empty. Please check it carefully.");
            }
        }
    }
3
  • After observing other answers, I think your BeforeSave method is being called at least two times. In this case, showing message box only once will require a flag which is outside of BeforeSave method. So, you should either post the code from where this method is being called or verify yourself that - you want to call BeforeSave multiple times. Commented Sep 26, 2016 at 4:57
  • @krw12572 I did try adding a flag as bellow those answer they giving but the result still same. Commented Sep 26, 2016 at 6:06
  • I've added another answer, see if it works for you now. Commented Sep 26, 2016 at 7:21

5 Answers 5

2

You can simply introduce a flag:

public void BeforeSave(BCE.AutoCount.Invoicing.Sales.SalesOrder.SalesOrderBeforeSaveEventArgs e)
{
    bool hasEmpty = false;

    for (int i = 0; i < e.MasterRecord.DetailCount; i++)
    {
        if (String.IsNullOrEmpty(e.MasterRecord.GetDetailRecord(i).YourPONo.ToString()))
        {
            hasEmpty = true;
        }
    }

    if (hasEmpty) {
        MessageBox.Show("You left Your PO No empty. Please check it carefully.");
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

If it's showing twice, then BeforeSave might be getting called twice?
@LeonBambrick if user key in 3 empty "po no" it will prompt 3 time.It will follow how many empty "PO No".. any suggestion?
2

Why not break out of the loop so that it stops checking?

public void BeforeSave(BCE.AutoCount.Invoicing.Sales.SalesOrder.SalesOrderBeforeSaveEventArgs e)
{
    for (int i = 0; i < e.MasterRecord.DetailCount; i++)
    {
        if (String.IsNullOrEmpty(e.MasterRecord.GetDetailRecord(i).YourPONo.ToString()))
        {
            MessageBox.Show("You left Your PO No empty. Please check it carefully.");
            break; // <--
        }
    }
}

return will also work in this situation.


Test if BeforeSave is running twice:

public void BeforeSave(BCE.AutoCount.Invoicing.Sales.SalesOrder.SalesOrderBeforeSaveEventArgs e)
{
    MessageBox.Show("Test"); // <--
    for (int i = 0; i < e.MasterRecord.DetailCount; i++)
    {
        if (String.IsNullOrEmpty(e.MasterRecord.GetDetailRecord(i).YourPONo.ToString()))
        {
            MessageBox.Show("You left Your PO No empty. Please check it carefully.");
            break; // <--
        }
    }
}

As yo can see I added a new "Test" message at the top of the method (outside the loop), if you see duplicate "Test" messages when using the code, it means that BeforeSave is running twice.

In that case you need to look at why it is running twice, and then fix that. If that is not fixable, then there could be some syncronization solution... such as:

private int canSave;

public void BeforeSave(BCE.AutoCount.Invoicing.Sales.SalesOrder.SalesOrderBeforeSaveEventArgs e)
{
    if (Interlocked.CompareExchange(ref canSave, 0, 1) != 1)
    {
        // Any cancelation logic that's appropiate here
        return;
    }
    for (int i = 0; i < e.MasterRecord.DetailCount; i++)
    {
        if (String.IsNullOrEmpty(e.MasterRecord.GetDetailRecord(i).YourPONo.ToString()))
        {
            MessageBox.Show("You left Your PO No empty. Please check it carefully.");
            break; // <--
        }
    }
}

Then you see canSave 1 to allow the code to run 0 to disallow it. The Interlocked operation will ensure that the code in BeforeSave will not run again until you set canSave to 1 somewhere in the code (it automatically sets it to 0 when it gets executed - no chance for multiple threads to mess it up).

Although I'm giving you a solution to control double execution of BeforeSave, if it is running twice than expected shows that there is some problem somewhere else, and you should be trying to fix that (unless it is third party code).

4 Comments

@ChengWan well, that's odd. I don't see why the code would continue in the loop after using break. I would think that there is some other code showing the message, maybe this "BeforeSave" method is running twice.
@Theroat firstly i'm appreciated what you explain to me. I did try MessageBox.Show("Test"); // <--but it didn't show up.
@ChengWan didn't show up - not even once - and you did nothing differect? On what enviroment are you running the code? because now what I think is that you are running outdated code - that is, that the solutions posted haven't worked because the code failed to compile or failed to deploy (and thus you ended up testing an old version). Edit: debug with break points, put a break point at the start of BeforeSave - if you are running a different version the IDE will complain that the version of the source doesn't match, it will also tell you if BeforeSave is running multiple times.
@Theroat ya, it didn't show up not even once. I try to write in a software name "autocount" just some customize part. I do everytime I write will save and reopen the software.
1

Either use a flag, or use Linq.

public void BeforeSave(BCE.AutoCount.Invoicing.Sales.SalesOrder.SalesOrderBeforeSaveEventArgs e) {
    bool flag = false;
    for (int i = 0; i < e.MasterRecord.DetailCount; i++) 
    { 
        if (String.IsNullOrEmpty(e.MasterRecord.GetDetailRecord(i).YourPONo.ToString())) 
        { 
            flag = true;
            break;
        } 
    } 
    if (flag)
        MessageBox.Show("You left Your PO No empty. Please check it carefully.");
}

I don't know enough of the used objects to offer a linq solution

1 Comment

Bro thank for your effort.. I did try your code run well but the result still same prompt out 2 dialog.. any other solution?
1

Following way should work for you even if your BeforeSave method is getting called multiple times.

private bool _isMessageBoxShown;
public void BeforeSave(BCE.AutoCount.Invoicing.Sales.SalesOrder.SalesOrderBeforeSaveEventArgs e) 
{    
  for (int i = 0; i < e.MasterRecord.DetailCount; i++) 
  { 
    if (String.IsNullOrEmpty(e.MasterRecord.GetDetailRecord(i).YourPONo.ToString())) 
    { 
        if(!_isMessageBoxShown)
        {
           _isMessageBoxShown = true;
           MessageBox.Show("You left Your PO No empty. Please check it carefully.");
           break;
        }
    } 
  }            
}

Just make sure that when you want your messagebox to be shown next time, you will need to set _isMessageBoxShown = false;.

1 Comment

bro I tried already. is that a possible it's a "yes/no"dialog if the dialog show and i close it. so it will keep repeat call it out?
0

I found out another way to solve it.

public void BeforeSave(BCE.AutoCount.Invoicing.Sales.SalesOrder.SalesOrderBeforeSaveEventArgs e)
    {
        int tt = 0;
        for (int i = 0; i < e.MasterRecord.DetailCount; i++)
        {
            if (tt == 0)
            {
                if (String.IsNullOrEmpty(e.MasterRecord.GetDetailRecord(i).YourPONo.ToString()))
                {
                    MessageBox.Show("You left Your PO No empty. Please check it carefully.");
                    tt = 1;
                }
            }
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.