I'm developing an application for a windows CE 6.0 scanner device and I'm having an issue when I want to insert datas. I'm using the OpenNETCF.ORM framework and SQLite. The problem happens when I want to add a 3rd article to the database with the insert() method. It works well with the first 2 articles but when it comes to the 3rd one, it throws an exception which is "SQLiteConnection".
Did anyone have the same issue with this ORM framework and could help me pls?
Here is the code of DatabaseHelper.cs :
using System;
using System.Windows.Forms;
using OpenNETCF.ORM;
namespace OfflineWMS
{
public class DatabaseHelper
{
private Scanner _scannerManager = new Scanner();
private static DatabaseHelper _instance = null;
private static readonly object MyLock = new object();
private SQLiteDataStore _stockDatabase;
public SQLiteDataStore StockDatabase1
{
get { return _stockDatabase; }
set { _stockDatabase = value; }
}
private DatabaseHelper() {}
public static DatabaseHelper getInstance()
{
lock (MyLock)
{
if (_instance == null)
{
_instance = new DatabaseHelper();
_instance.InitDatabase();
}
return _instance;
}
}
//Create the Database
public void InitDatabase()
{
StockDatabase1 = new SQLiteDataStore(ConfigurationHelper.Settings["PathToTheApp"] + ConfigurationHelper.Settings["DatabaseFilename"]);
if (!StockDatabase1.StoreExists)
{
StockDatabase1.CreateStore();
}
InitializeDbStructure();
}
private void InitializeDbStructure()
{
StockDatabase1.AddType<Article>();
}
}
}`
The code in the Form.cs where I have the issue (at the end of the code) :
using System;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Threading;
using System.Windows.Forms;
using FileOperations.CsvImport;
using FileOperations.CsvImport.FluentInterface;
using OfflineWMS.Properties;
using OpenNETCF.ORM;
using Symbol.Barcode;
namespace OfflineWMS
{
public partial class Form1 : Form
{
private ReaderData TheReaderData;
private Scanner ScannerManager;
private bool IsReaderInitiated;
private DatabaseHelper db;
public Form1()
{
InitializeComponent();
}
protected override void OnClosing(CancelEventArgs e)
{
if (IsReaderInitiated)
ScannerManager.TermReader();
base.OnClosing(e);
}
private void ScannerManager_ReadNotify(object sender, EventArgs e)
{
TheReaderData = ScannerManager.Reader1.GetNextReaderData();
//action to do
ProcessData(TheReaderData);
ScannerManager.StopRead();
ScannerManager.StartRead(true);
}
private void ProcessData(ReaderData TheReaderData)
{
if (TheReaderData != null)
{
textBox1.Text = TheReaderData.Text;
}
else
{
MessageBox.Show(Resources.ReaderDataIsNull);
}
}
private void ScannerManager_StatusNotify(object sender, EventArgs e)
{
BarcodeStatus theStatus = ScannerManager.Reader1.GetNextStatus();
if (theStatus != null)
{
switch (theStatus.State)
{
case States.IDLE:
statusBar1.Text = Resources.IDLEStatus;
break;
case States.READY:
statusBar1.Text = Resources.ReadyStatus;
break;
case States.WAITING:
statusBar1.Text = Resources.WaitingStatus;
break;
default:
statusBar1.Text = Resources.DefaultStatus;
break;
}
}
else
{
//log error
}
}
protected override void OnLoad(EventArgs e)
{
// DatabaseHelper is instanciated only once here
db = DatabaseHelper.getInstance();
ScannerManager = new Scanner();
IsReaderInitiated = ScannerManager.InitReader();
textBox1.Focus();
// if not initialized, we quit the app
if (!IsReaderInitiated)
{
MessageBox.Show(Resources.InitiatingError);
Application.Exit();
}
else
{
ScannerManager.AttachStatusNotify(ScannerManager_StatusNotify);
ScannerManager.StartRead(true);
ScannerManager.AttachReadNotify(ScannerManager_ReadNotify);
}
//db.InitDatabase();
base.OnLoad(e);
}
private void buttonAddToDb_Click(object sender, EventArgs e)
{
if ((TheReaderData != null))
{
var article = new Article
{
ArticleName = "test",
ArticleCode = "321",
ArticleFeature = "true blue",
ArticlePrice = 33.22m,
ArticleQuantity = 15,
ArticleBarcode = TheReaderData.Text
};
var article2 = new Article
{
ArticleName = "test",
ArticleCode = "321",
ArticleFeature = "true blue",
ArticlePrice = 33.22m,
ArticleQuantity = 15,
ArticleBarcode = TheReaderData.Text
};
var article3 = new Article
{
ArticleName = "test",
ArticleCode = "321",
ArticleFeature = "true blue",
ArticlePrice = 33.22m,
ArticleQuantity = 15,
ArticleBarcode = TheReaderData.Text
};
// HERE
try
{
db.StockDatabase1.Insert(article);// article added
db.StockDatabase1.Insert(article2);// article2 added
db.StockDatabase1.Insert(article3);// throws the exception "SQLiteException"
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
ScannerManager.TermReader();
Application.Exit();
}
else
{
MessageBox.Show(Resources.ScanData);
}
}
}
}
And this is the stacktrace of this SQLiteException :
at System.Data.SQLite.SQLiteConnection.CheckDisposed()
at System.Data.SQLite.SQLiteConnection.get_State()
at OpenNETCF.ORM.SQLStoreBase`1.<GetPoolConnection>b__11(IDbConnection c)
at System.Linq.Enumerable.<WhereIterator>d__0`1.MoveNext()
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
at OpenNETCF.ORM.SQLStoreBase`1.GetPoolConnection()
at OpenNETCF.ORM.SQLStoreBase`1.GetConnection(Boolean maintenance, Boolean isRetry)
at OpenNETCF.ORM.SQLStoreBase`1.GetConnection(Boolean maintenance)
at OpenNETCF.ORM.SQLiteDataStore.OnInsert(Object item, Boolean insertReferences)
at OpenNETCF.ORM.DataStore`1.Insert(Object item, Boolean insertReferences, Boolean recoveryInsert)
at OpenNETCF.ORM.DataStore`1.Insert(Object item, Boolean insertReferences)
at OpenNETCF.ORM.DataStore`1.Insert(Object item)
at OfflineWMS.Form1.buttonAddToDb_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.ButtonBase.WnProc(WM wm, Int32 wParam, Int32 lParam)
at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)
at System.Windows.Forms.Application.Run(Form fm)
at OfflineWMS.Program.Main()