Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SEHException on OleDb connection open

I'm developing a small application that will simplify logging, it does so by adding some inputs to an MS Access database through OleDB.

let private conn = new OleDbConnection(connectionString)

let private submitCmd date wins = 
    let cmd = new OleDbCommand("INSERT INTO ArenaStats ([Date], [Wins]) VALUES (@Date, @Wins)", 
                                Connection = conn, CommandType = CommandType.Text)
    ["@Date", box date; "@Wins", box wins]
    |> List.iter (cmd.Parameters.AddWithValue >> ignore)
    cmd


let private submit date wins =
    try
        conn.Open()
        (submitCmd date wins).ExecuteNonQuery() |> ignore
    finally
        conn.Close()

[<CompiledName "AddEntry">]
let addEntry(date:DateTime, wins:int) =
    submit date wins

Now testing this through FSI works just as expected. However, when I consume this API from a C# WPF project it will throw an SEHException at conn.Open(). I am really scratching my head over why this is happening.

Edit

As suggested, I have also tried to implement the same code purely in C# and in the same project, it will throw the same exception at the same place but I am posting the code below for reference.

class MsAccessDatabase : IArenaWinsDatabase {
        private OleDbConnection connection = new OleDbConnection(connectionString);

        private OleDbCommand SubmitCommand(DateTime date, int wins) {
            return new OleDbCommand("INSERT INTO ArenaStats ([Date], [Wins]) VALUES (@Date, @Wins)") {
                Connection = connection,
                CommandType = System.Data.CommandType.Text,
                Parameters = {
                    new OleDbParameter("@Date", date),
                    new OleDbParameter("@Wins", wins)
                }
            };
        }

        public void Submit(DateTime date, int wins) {
            try {
                connection.Open();
                SubmitCommand(date, wins).ExecuteNonQuery();
            }
            finally {
                connection.Close();
            }
        }
    }
like image 306
Overly Excessive Avatar asked Jun 22 '15 08:06

Overly Excessive


2 Answers

With some help from Philip I was able to figure it out. It seems that by default FSI is configured to run in 64-bit by default while the WPF project is set to "prefer 32-bit". Changing the target platform for the WPF project to 64-bit resolved the issue.

like image 176
Overly Excessive Avatar answered Oct 31 '22 04:10

Overly Excessive


When trying to run the following code:

var connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties=Excel 12.0;", FilePath);
OleDbConnection OleDbConnection = new System.Data.OleDb.OleDbConnection(connectionString);
OleDbConnection.Open();

An SEHException exception is thrown at runtime, with the error message 'External Component has thrown an Exception'

This will usually occur when the build configuration platform in Visual Studio is incorrect, this can occur in both build configuration platforms, x86 and x64.

This is due to a mismatch between the build configuration platform of your project and the Microsoft Access Database Engine which is installed on your machine.

In order to resolve this error:

  • Change the build configuration platform in Visual Studio - make sure it matches the Microsoft Access Database Engine version on your machine
  • Recompile and run your project
  • The run time error should now be resolved
like image 20
Merav Kochavi Avatar answered Oct 31 '22 03:10

Merav Kochavi