Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 5th, 2005, 12:19 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default Catching exceptions thrown from an event...

There is probably a simple answer to this, but I cannot seem to find out where to catch exceptions thrown from an event, say from within a RowDeleting event of DataTable. I have done a small application with a DataView and when I delete a row I would like a confirmation dialog.

I have read at MSDN that one has the throw an exception in order to cancel the deletion of the row. Fine, but where do I catch this exception? It is the main thread so I am clueless. This is the method implementing the event actions...
Code:
        private void Table_RowDeleting(object sender, DataRowChangeEventArgs ea)
        {
            XmlDocument xml;
            DialogResult result;
            try
            {
                result = MessageBox.Show("Delete element for " + (string)ea.Row[1] + "?", 
                    "Deleting...", MessageBoxButtons.OKCancel);

                if(result == DialogResult.OK)
                {
                    if(File.Exists(tbFilename.Text))
                    {
                        xml = new XmlDocument();
                        xml.Load(tbFilename.Text);
                        xml.DocumentElement.RemoveChild(
                            xml.DocumentElement.SelectSingleNode("item[@id='" 
                                + (int)ea.Row[0] + "']"));
                        xml.Save(tbFilename.Text);
                    }
                    else
                        throw new FileNotFoundException(
                            "The specified file was not found.");
                }
                else
                    throw new Exception("canceled");
            }
            catch(Exception e)
            {
                MessageBox.Show(e.Message);
                throw;
            }            
        }
        If the exception throwing solution is not the way to cancel row deletion in a DataTable, please let me know as well.

Thanks,
Jacob.
__________________
Danish audio books for download at http://www.lytenbog.dk (Danske lydbøger til download).
 
Old December 5th, 2005, 06:06 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

If you r deleting the rows using DataRow's Delete function, then u put this function call in try-catch. Like this

            try
            {
                // Delete all the rows
                foreach( DataRow row in employeeTable.Rows )
                    row.Delete();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


Any exception thrown from within the row_deleting event handler will be caught here.
And yes- throwing the exception, indeed halts the deletion process.

Regards
Ankur Verma
 
Old December 6th, 2005, 06:50 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Ankur. I am not using the Delete method since the DataTable is shown as a DataView in the application and the RowDeleting event is therefore triggered when the Delete button is clicked and the row is highlightend.

There must be a way to catch these exceptions as well, right?

Thanks, Jacob.
 
Old December 6th, 2005, 10:39 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Finally, found the solution. The following code is the interesting parts of the solution...
Code:
        ...
        [STAThread]
        static void Main() 
        {
            Application.ThreadException += 
                new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            Application.Run(new MainForm());
        }
        ...
        private static void Application_ThreadException(object sender, 
            System.Threading.ThreadExceptionEventArgs ea)
        {
            MessageBox.Show("error");
        }
        ...
        private void Table_RowDeleting(object sender, DataRowChangeEventArgs ea)
        {
            XmlDocument xml;
            DialogResult result;
            try
            {
                result = MessageBox.Show("Delete element for " + (string)ea.Row[1] + "?", 
                    "Deleting...", MessageBoxButtons.OKCancel);

                if(result == DialogResult.OK)
                {
                    ...
                }
                else
                    throw new Exception("canceled");
            }
            catch(Exception e)
            {
                MessageBox.Show(e.Message);
                throw;
            }            
        }
        ...
I found the solution at this page.

Thanks anyway, Jacob.
 
Old December 6th, 2005, 12:38 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

Okay, so you are basically creating an exception handler for all the exceptions occuring at the main thread level. I was thinking on the line of putting the whole database operation in a new thread.
Other solution could be to trap the key press and do the deletion yourself calling the DataRow's Delete function based on the validation, when delete key is pressed.
But that seemed like a lot of work.
Nice solution, this one. Thanks for updating the forum.

Regards
Ankur Verma
 
Old December 6th, 2005, 12:48 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes, that would be possible too, thanks, regards, Jacob.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Database does not update, no exception thrown hexOffender VB Databases Basics 5 November 24th, 2006 03:28 PM
Autosuggest Exception thrown jellie BOOK: Professional Ajax ISBN: 978-0-471-77778-6 3 March 14th, 2006 10:19 AM
Exception thrown Invalid_Viewstate r_ganesh76 ASP.NET 1.0 and 1.1 Professional 1 December 2nd, 2005 04:32 PM
catching the OnSelectedIndexChanged event Aaron Edwards Classic ASP Professional 1 October 5th, 2005 02:51 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.