none
Try/Catch: Where should I put this CORRECT messagebox? RRS feed

  • Question

  • I do want to put a message after successfully updating database, but it cannot be inside try statement.

    If there is error, then just display err, no need additional messagebox.

    Thanks.

    try
                {
                    AdapterTest.Update(dSetTest);
                    //MessageBox("Changes are successfully updated.");
    //Where to put this message box, only when there is no ex.Message
                }
                catch (OdbcException ex)
                {
                    MessageBox.Show(ex.Message);
                }


    • Edited by VA_er Wednesday, March 20, 2019 8:53 PM
    Wednesday, March 20, 2019 8:43 PM

Answers

All replies

  • Why can't you display the message inside the 'try' block?  If the database update triggers an exception, the success message will not be displayed since execution will jump to the 'catch' block.


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    Wednesday, March 20, 2019 8:48 PM
  • I tried, but error message, see above screenshot.

    Shall I change it to messageboxbutton?

    Thanks.


    • Edited by VA_er Wednesday, March 20, 2019 8:56 PM
    Wednesday, March 20, 2019 8:53 PM
  • One idea is to have a base exception class so we can set a property (see mHasException) in a class to work with data then the caller checks to see if there was an exception (in this case a console app but works the same in a form) e.g. ops.IsSuccessFul.



    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, March 20, 2019 8:56 PM
    Moderator
  • Use "MessageBox.Show", not "MessageBox".

    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    Wednesday, March 20, 2019 8:57 PM
  • Thanks, still confuse with VBA syntax. Forgot Show.
    • Edited by VA_er Wednesday, March 20, 2019 9:00 PM
    Wednesday, March 20, 2019 8:59 PM
  • I do want to put a message after successfully updating database, but it cannot be inside try statement.

    If there is error, then just display err, no need additional messagebox.

    Thanks.

    try
                {
                    AdapterTest.Update(dSetTest);
                    //MessageBox("Changes are successfully updated.");
    //Where to put this message box, only when there is no ex.Message
                }
                catch (OdbcException ex)
                {
                    MessageBox.Show(ex.Message);
                }


    Why use a try/catch anywhere in the code? You could use global exception handling/centralized exception handling and let all exceptions be caught and handled  there.

    https://www.codeproject.com/Articles/43182/Centralised-Exception-Handling-in-C-Windows-Applic

    Wednesday, March 20, 2019 9:16 PM
  • Hi VA_er,

    Based on your description, it seems that Dave Doknjas' reply resolve your issue, could you please mark it answer, it will beneficial to other communities who have the similar issue.

    Best regards,

    Zhanglong


    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, March 21, 2019 6:17 AM
    Moderator