SEHException on OleDb connection open
Asked Answered
P

3

13

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();
            }
        }
    }
Pantin answered 22/6, 2015 at 8:57 Comment(7)
Have you tried replacing it with the equivalent C# code within your WPF project and seeing if the error still occurs?Mulish
What does the exception say?Krenn
@FyodorSoikin External Component has thrown an Exception.Pantin
@AlexFoxGill Same exception, I added the code for reference.Pantin
What about ErrorCode? InnerException? Data?Krenn
@OverlyExcessive seems like something different between fsi & the WPF process (1) in the WPF app is the query run on an STAThread? (2) is one running in 32-bit mode & the other in 64-bit?Merimerida
@PhillipTrelford It seems you hit the nail on the head! FSI is set to run in 64-bit by default and it seems my WPF project was set to target 32-bit. Changing the platform target made it work!Pantin
P
10

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.

Pantin answered 24/6, 2015 at 19:8 Comment(0)
B
2

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
Battaglia answered 15/10, 2015 at 5:50 Comment(0)
S
0

I know this is a really old thread, but I just ran into the same problem with a different solution.

When I installed the Access Database Engine 2016 Redistributable (x64) for the ACE provider the installer gave me an error that VCRUNTIME140.DLL wasn't installed, but the installer completed anyways and the ACE provider was available.

Uninstalling the Access Database Engine, installing the VC++ 2015 Redistributable R3 (https://www.microsoft.com/en-us/download/confirmation.aspx?id=52685), then re-installing the Access Database Engine solved the problem.

Scintillate answered 25/3, 2021 at 11:51 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.