none
inner exception RRS feed

  • Question

  • I've written a delete method to delete a data(ms access), it gives me an exception message.  Tried to debug but could not find in my code.  The message says an "inner exception".  What could be the cause?

    
    
    
    
    
    Tuesday, November 28, 2017 6:08 AM

All replies

  • cause of inner exception

    Tuesday, November 28, 2017 5:57 AM
  • Exceptions have an InnerException property, which may contain details of an exception that occurred deeper in the code.

    You need to examine your caught exception and look at this property and you will probably find this contains another Exception with its own message, which may have its own InnerException set, and so on. So you may have to dig down.

    If this doesn't lead you to an answer, please post the full message of the exception (and all inner exceptions) and any relevant code examples.

    Tuesday, November 28, 2017 8:09 AM
  • What are you needing here?
    Tuesday, November 28, 2017 12:30 PM
  • This is a duplicate; the question was asked with a better explanation in the C# forum.


    Sam Hobbs
    SimpleSamples.Info

    Tuesday, November 28, 2017 9:49 PM
  • Use "try" and "catch" to catch the error and show an error message. As RJP1973 says, you might need to also show the inner exception.


    Sam Hobbs
    SimpleSamples.Info

    Tuesday, November 28, 2017 9:52 PM
  • Here is something to try. I placed the delete operation in a class which in a real app would have read, add, edit and delete operations.

    Other than this is it's really best to learn how to use break-points and the debugger.

    If there is an error you can integrate it from the caller in a form.

    using System;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                var ops = new Operations();
                var someId = 10;
                if (!ops.DeleteCustomerByIdentifier(someId))
                {
                    if (ops.HasException)
                    {
                        MessageBox.Show($"Error: {ops.ExceptionMessage}{Environment.NewLine}Inner Exception: {ops.InnerExceptionMessage}");
                    }
                }
    
            }
        }
    }
    

    Data class and a class for helper extension methods (which are kind of overkill as in development mode we can simply set a break-point and inspect the Inner Exception, unwind it if it's deep which I doubt)

    using System;
    using System.Collections.Generic;
    using System.Data.OleDb;
    using System.IO;
    using System.Linq;
    
    namespace WindowsFormsApplication1
    {
        public class Operations
        {
            private OleDbConnectionStringBuilder Builder = new OleDbConnectionStringBuilder
            {
                DataSource = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CustomersDatabase.accdb"),
                Provider = "Microsoft.ACE.OLEDB.12.0"
            };
    
            public bool HasException { get; set; }
            public Exception Exception { get; set; }
            public string ExceptionMessage
            {
                get
                {
                    if (HasException)
                    {
                        return Exception.Message;
                    }
                    else
                    {
                        return "None";
                    }
                }
            }
            public string mInnerExceptionMessage;
            public string InnerExceptionMessage { get { return mInnerExceptionMessage; } }
            public bool DeleteCustomerByIdentifier(int pIdentifier)
            {
                bool success = true;
                HasException = false;
    
                try
                {
                    using (OleDbConnection cn = new OleDbConnection { ConnectionString = Builder.ConnectionString })
                    {
                        using (OleDbCommand cmd = new OleDbCommand { Connection = cn })
                        {
    
                            cmd.CommandText = "DELETE FROM Customers WHERE Identifier = @Identifier";
                            cmd.Parameters.AddWithValue("@Identifier", pIdentifier);
    
                            cn.Open();
    
                            success = cmd.ExecuteNonQuery() == 1;
                        }
                    }
                }
                catch (Exception ex)
                {
                    HasException = true;
                    Exception = ex;
                    mInnerExceptionMessage = ex.GetaAllMessages();
                    success = false;
                }
    
                return success;
            }
        }
    
        public static class Extensions
        {
            public static IEnumerable<TSource> FromHierarchy<TSource>(this TSource source,Func<TSource, TSource> nextItem,Func<TSource, bool> canContinue)
            {
                for (var current = source; canContinue(current); current = nextItem(current))
                {
                    yield return current;
                }
            }
            public static IEnumerable<TSource> FromHierarchy<TSource>(
                this TSource source,
                Func<TSource, TSource> nextItem)
                where TSource : class
            {
                return FromHierarchy(source, nextItem, s => s != null);
            }
            public static string GetaAllMessages(this Exception exception)
            {
                var messages = exception.FromHierarchy(ex => ex.InnerException).Select(ex => ex.Message);
                return string.Join(Environment.NewLine, messages);
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, November 28, 2017 10:35 PM
    Moderator