BadImageFormatException with SQLite in ASP.NET razor website

0

I'm trying to create a very simple webshop in Razor with SQLite. Unfortunately, when i trying to create the database (or creating the SQLiteConnection object) it throws a strange System.BadImageFormatException.

An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)"} System.BadImageFormatException

My code looks similar as the following:

public bool CreateDatabase()
{
try
{
  string db = HttpContext.Current.Server.MapPath("~/App_Data/mydb.sqlite");
  SQLiteConnection.CreateFile(db);
  SQLiteConnection m_dbConnection = new SQLiteConnection(@"Data Source="+ db + ";Version=3;");
  m_dbConnection.Open();

  string sql = "create TABLE cart (UserHash varchar(35), imageid varchar(255), rider varchar(255), competition varchar(255), usagetype varchar(255), retouch varchar(10), blacknwhite varchar(10))";
  SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
  command.ExecuteNonQuery();
  return true;
}
catch (Exception)
{
  return false;
}
}

This website probably will run on third-party hosting, so it's essential to use a relative path for the sqlite file location, although i couldn't get it work so far.

In db variable, i get the correct App_Data folder location. I get the exception at new SQLiteConnection. I also tried using the following connectionstring, no luck:

"Data Source=|DataDirectory|mydb.sqlite; Version=3;"

What am i missing?

c#
asp.net
sqlite
razor
asked on Stack Overflow Oct 13, 2018 by Jancsik Zsolt

1 Answer

0

BadImageFormatException happens when your application and your libraries use a different bitness. For example, the application is built for AnyCPU (without prefer 32bit) or x64 and your libraries are compiled for 32bit or viceversa.

Usually the solution is very simple. Just copy the appropriate version of SQLite for the bitness of your app to your run folder or change the Target Platform of your app to match the library version used

answered on Stack Overflow Oct 14, 2018 by Steve

User contributions licensed under CC BY-SA 3.0