none
How to "Save a list of errors and display at the end" and "display error message then exit when encountering the first error"? RRS feed

  • Question

  • For learning purpose, I would like to know how to modify below sample code for both options of displaying error message (as mentioned in the title). Does MessageBox have character limit? What if there are a lot of errors?

    As newbie, I am not very sure if below code is for which one of those two options.

    Thanks.

    foreach (DataRow tempRow in table.Rows)
    {
    
    try
    {
    ExecuteSqlCommand(Cn, query);
    }
    
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    
    }

    • Moved by CoolDadTx Friday, April 12, 2019 6:40 PM Winforms related
    Friday, April 12, 2019 2:12 PM

Answers

  • Hi VA_er,

    >> Take my sample code for example, I mean exit the For loop.

    Try to use the keyword "return". Just like the code shown as follows:

        List<int> list = new List<int> { 1, 2, 3, 4 };
        foreach (var i in list)
        {
            if (i < 3)
                MessageBox.Show(i.ToString());
            // If i >= 3 exit the loop
            else
                return;
        }

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 17, 2019 1:29 AM
    Moderator

All replies

  • MessageBox displays everything as simple text so trying to do "lines" is only going to work well for a few lines. After that it just doesn't scale. Task dialogs were added in Vista and were designed to replace message boxes as a generally reusable dialog for displaying stuff with some added functionality. I would lean toward using that instead if you expect lots of lines. Unfortunately .NET doesn't expose it directly so you have to use the library MS put together to support it.

    However I would probably lean toward the fact that nobody is going to care about the error after the first line or so anyway. So a preferable route is to show a more generic method in most cases and consider having a "View details" or equivalent link that allows someone to get the actual exception details. At that point you can have a custom form that displays as much as you want (in a scrolling text editor for example). Of course getting a details link on the form isn't possible with MessageBox so you'll either need to use task dialog or create your own form either way.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, April 12, 2019 6:40 PM
  • CoolDadTx: Why you move so many threads around? Quite some of my threads have been moved before. I do feel like I posted it in the appropriate forum (C# forum). I don't know why it has been moved to Window Form forum.
    • Edited by VA_er Friday, April 12, 2019 6:48 PM
    Friday, April 12, 2019 6:48 PM
  • If your question is only applicable to a UI (such as Winforms) then it belongs in Winforms. The C# forum is for questions specifically related to using C# and not tied to anything else like WCF, UI, web development etc. Since your question is specifically about how much you can use MessageBox (which is winforms specific) then it belongs in Winforms in my opinion. The answer to your question would be different if you were using WPF, UWP, etc.

    Michael Taylor http://www.michaeltaylorp3.net

    Friday, April 12, 2019 7:03 PM
  • Hi VA_er,

    >> Save a list of errors and display at the end

    C# cannot get all possible exceptions directly. What you can do is using "try...catch..." around each code that may have an exception, and store the exception string in a List and display it at the end.

    >> Does MessageBox have character limit?

    There is no limit to the use of MessageBox. For more info, you can refer to MessageBox Class.

    >> display error message then exit when encountering the first error

    To exit, you can use "Application.Exit();";

    >> What if there are a lot of errors?

    You can only get the possible exceptions one by one.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 15, 2019 3:26 AM
    Moderator

  • >> display error message then exit when encountering the first error

    To exit, you can use "Application.Exit();";



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    // Sorry that I did not make my question clear. I did not mean exit application. Take my sample code for example, I mean exit the For loop.

    Thanks.
    Tuesday, April 16, 2019 4:11 PM
  • Hi VA_er,

    >> Take my sample code for example, I mean exit the For loop.

    Try to use the keyword "return". Just like the code shown as follows:

        List<int> list = new List<int> { 1, 2, 3, 4 };
        foreach (var i in list)
        {
            if (i < 3)
                MessageBox.Show(i.ToString());
            // If i >= 3 exit the loop
            else
                return;
        }

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 17, 2019 1:29 AM
    Moderator
  • Hi VA_er,

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer.So that it will help other members to find the solution quickly if they face the similar issue.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 18, 2019 6:37 AM
    Moderator