I've got this code to insert 1..N records in a SQLite database in a Windows CE / Compact Framework app:
void IPlatypusDBUtils.SaveSiteMappingData(IEnumerable<SiteMapping> siteMappings)
{
ExceptionLoggingService.Instance.WriteLog("Reached TestPlatypusDBUtils.SaveSiteMappingData");
IEnumerable<SiteMapping> siteMaps = siteMappings;
try
{
using (SQLiteConnection conn = new SQLiteConnection(PlatypusUtils.GetDBConnection()))
{
conn.Open();
foreach (SiteMapping sm in siteMaps)
{
using (SQLiteCommand cmd = new SQLiteCommand(conn))
{
cmd.CommandText =
@"INSERT INTO SiteMapping (Id, SiteNumber, LocationNumber, SiteName) VALUES (@Id, @SiteNumber, @LocationNumber, @SiteName)";
cmd.Parameters.Add(new SQLiteParameter("LocationNumber", sm.Id));
cmd.Parameters.Add(new SQLiteParameter("SiteNumber", sm.SiteNumber.ToString())); // or Platypusconsts.currentsitenum ?
cmd.Parameters.Add(new SQLiteParameter("LocationNumber", sm.LocationNumber));
cmd.Parameters.Add(new SQLiteParameter("SiteName", sm.SiteName));
cmd.ExecuteNonQuery();
} // using (SQLiteCommand cmd
} // foreach (SiteMapping sm in siteMaps)
conn.Close();
} // using (SQLiteConnection conn
} // try
catch (Exception ex)
{
String msgInnerExAndStackTrace = String.Format(
"{0}; Inner Ex: {1}; Stack Trace: {2}", ex.Message, ex.InnerException, ex.StackTrace); ExceptionLoggingService.Instance.WriteLog(String.Format("Exception in TestPlatypusDBUtils.SaveSiteMappingData: {0}", msgInnerExAndStackTrace));
} // catch
}
...but it fails to insert any records. The log file contains "Reached TestPlatypusDBUtils.SaveSiteMappingData" (the Insert method is reached) but not "Exception in TestPlatypusDBUtils.SaveSiteMappingData". Is ExecuteNonQuery() not the right call for an Insert, or what is the problem?
UPDATE
I added this just below the first log write:
MessageBox.Show(String.Format("siteMappings about to be inserted into SiteMapping table: {0}", siteMappings));
...and I see this:
I don't know if this means there's actually nothing in SiteMappings, or that I'm trying to view the contents in the wrong way. I first appended a ".ToString()" but it was grayed out, so thought it was moot and would be mute.
UPDATE 2
Apparently the fundamental problem is that no SiteMappings are being passed to the method. I added this:
foreach (SiteMapping sm in siteMaps)
{
MessageBox.Show(String.Format("this siteMap is {0}", sm));
...and never see "this siteMap is [bla]".
For future reference, the scream shot above shows what's contained in an "empty" IEnumerable.
UPDATE 3
It turns out it was a data problem (GIGO). Or, garbage trying to go in doesn't go in.