INSERT Query Problem

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for SQLite
Post Reply
aliirz
Posts: 1
Joined: Fri 10 Dec 2010 12:49

INSERT Query Problem

Post by aliirz » Fri 10 Dec 2010 13:23

hey guys

i am trying to insert data inside a table but i keep on getting this exception:

A first chance exception of type 'Devart.Data.SQLite.SQLiteException' occurred in Devart.Data.dll
Parameter '18' is missing

here is my code:

Code: Select all

   SQLiteConnection con = new SQLiteConnection();
            SQLiteCommand cmd = new SQLiteCommand();
            string dblocation = System.AppDomain.CurrentDomain.BaseDirectory;
            dblocation = dblocation + @"test.s3db";
            con.ConnectionString = @"Datasource=" + dblocation;
            cmd.CommandText = "INSERT INTO ErrorLog(TheTimestamp,ErrorCategory,VariableValues,ExceptionOccured,FunctionName) VALUES(" + Convert.ToString(DateTime.Now) + "," + categoryString + "," + variablesValue + "," + Convert.ToString(theException) + "," + functionAndErrorPoint +")";
            cmd.Connection = con;
            con.Open();
            
            try
            {
                rowsaffected = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return;
            }
heres my table:

CREATE TABLE [ErrorLog] (
[ErrorId] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
[TheTimestamp] DATE NOT NULL,
[ErrorCategory] NVARCHAR(50) NOT NULL,
[VariableValues] NVARCHAR(500) NOT NULL,
[ExceptionOccurred] NVARCHAR(500) NOT NULL,
[FunctionName] NVARCHAR(500) NOT NULL,
[ClientID] INTEGER NULL,
[Username] NVARCHAR(50) NULL
)

please help me out. what am i doing wrong here??? consider all variables and strings non-empty and declared. awaiting your replies.

regards

StanislavK
Devart Team
Posts: 1710
Joined: Thu 03 Dec 2009 10:48

Post by StanislavK » Mon 13 Dec 2010 17:23

The problem is that the string literals included into this SQL command are not quoted. You can either quote them, like

Code: Select all

cmd.CommandText = "INSERT INTO ErrorLog(TheTimestamp,ErrorCategory,VariableValues,ExceptionOccured,FunctionName) VALUES('" + Convert.ToString(DateTime.Now) + "'," + ...
or use SQL parameters (this is the recommended way). For the detailed information about using parameters in SQLiteCommands, please refer to
http://www.devart.com/dotconnect/sqlite ... eters.html

Feel free to contact us if you encounter any other problems when using dotConnect for SQLite.

Post Reply