Page 1 of 1

INSERT Query Problem

Posted: Fri 10 Dec 2010 13:23
by aliirz
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

Posted: Mon 13 Dec 2010 17:23
by StanislavK
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.