I'm getting this error message:
The type initializer for 'Microsoft.Data.Sqlite.SqliteConnection' threw an exception.
To be more explicit, I get:
Message = The type initializer for 'Microsoft.Data.Sqlite.SqliteConnection' threw an exception.
Inner Exception = System.Exception: You need to call SQLitePCL.raw.SetProvider(). If you are using a bundle package, this is done by calling SQLitePCL.Batteries.Init(). at SQLitePCL.raw.get_Provider() at SQLitePCL.raw.sqlite3_win32_set_directory(Int32 typ, String path) at Microsoft.Data.Sqlite.Utilities.BundleInitializer.Initialize() at Microsoft.Data.Sqlite.SqliteConnection..cctor()
Stack Trace = at Microsoft.Data.Sqlite.SqliteConnection..ctor(String connectionString) at CartographerYou.MainPage.InsertMapRecord(String mapName, String mapNotes, Int32 preferredZoomLevel) at CartographerYou.MainPage.btnCre8NewMap_Click(Object sender, RoutedEventArgs e)
These are the two methods (an event handler and a custom method) that are mentioned in the StackTrace:
private async void btnCre8NewMap_Click(object sender, RoutedEventArgs e)
{
try
{
string mapName = string.Empty;
string mapNotes = string.Empty;
int defaultZoomLevel = 1;
ClearLocations();
// Popul8 the cmbx
for (int i = 1; i < 20; i++)
{
cmbxCre8MapZoomLevels.Items.Add(i.ToString());
}
ContentDialogResult result = await cntDlgCre8Map.ShowAsync();
if (result == ContentDialogResult.Primary)
{
mapName = txtbxMapName.Text;
mapNotes = txtbxMapNotes.Text;
defaultZoomLevel = cmbxCre8MapZoomLevels.SelectedIndex + 1;
// select "Step Into Specific" from context menu
await InsertMapRecord(mapName, mapNotes, defaultZoomLevel);
}
// else do nothing (don't save)
}
catch (Exception ex)
{
string excMsg = string.Format("{0} Inner Exception: {1} Stack Trace: {2}", ex.Message, ex.InnerException, ex.StackTrace);
MessageDialog exceptionMsgDlg = new MessageDialog(excMsg, "btnCre8NewMap_Click");
await exceptionMsgDlg.ShowAsync();
}
}
private async Task InsertMapRecord(string mapName, string mapNotes, int preferredZoomLevel)
{
path = folder.Path;
connStr = string.Format(connStrBase, path);
try
{
using (SqliteConnection conn = new SqliteConnection(connStr))
{
String query = "INSERT INTO dbo.CartographerMain " +
"(MapName, MapNotes, PreferredZoomLevel) " +
"VALUES (@MapName, @MapNotes, @PreferredZoomLevel)";
using (SqliteCommand cmd = new SqliteCommand(query, conn))
{
cmd.Parameters.AddWithValue("@MapName", mapName);
cmd.Parameters.AddWithValue("@MapNotes", mapNotes);
cmd.Parameters.AddWithValue("@PreferredZoomLevel", preferredZoomLevel);
await conn.OpenAsync();
int result = await cmd.ExecuteNonQueryAsync();
if (result < 0)
{
MessageDialog dialog = new MessageDialog("Error inserting data into CartographerMain");
await dialog.ShowAsync();
}
}
}
}
catch (SqliteException sqlex)
{
string sqlExcMsg = string.Format("{0} Inner Exception: {1} Stack Trace: {2}", sqlex.Message, sqlex.InnerException, sqlex.StackTrace);
MessageDialog dialog = new MessageDialog(sqlExcMsg, "InsertMapRecord");
await dialog.ShowAsync();
}
}
Based on the Inner Exception about needing to call SQLitePCL.raw.SetProvider(), I changed the code from this:
using (SqliteConnection conn = new SqliteConnection(connStr))
{
. . .
...to this:
SqliteConnection conn = new SqliteConnection(connStr);
SQLitePCL.raw.SetProvider();
. . .
...but I don't know what I need to pass to SetProvider() - if this is even really the true solution to the problem.
This is what I get with that code:
What needs to be passed to SetProvider()?