简体   繁体   中英

{“Value cannot be null.\r\nParameter name: s”}

I am trying to create a stand alone program using a sqlite database but it keeps showing the following error every time I insert. I checked my parameters and none of them are null. Here is my insert code.

public bool insertToTable(SQLiteConnection sql,string TableName, string Date, string Receipt, string Particulars, string Description, string Category, double amount)
{
    SQLiteCommand command = new SQLiteCommand(sql);
    if (sql == null)
        return false;
    string statement = 
        "INSERT INTO @table (Date, Receipt,Particulars,Description,Category,Amount) " +
        "VALUES (@date, @rec,@part,@desc,@cat,@amt)";

    command.Parameters.AddWithValue("@date",Date);
    command.Parameters.AddWithValue("@rec", Receipt);
    command.Parameters.AddWithValue("@part", Particulars);
    command.Parameters.AddWithValue("@desc", Description);
    command.Parameters.AddWithValue("@cat", Category);
    command.Parameters.AddWithValue("@amt", amount);
    command.Prepare();
    int x = command.ExecuteNonQuery();
    return true;
}

and the stack trace is

 at System.Text.UTF8Encoding.GetByteCount(String chars)
   at System.Data.SQLite.SQLiteConvert.ToUTF8(String sourceText)
   at System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn, String strSql, SQLiteStatement previous, UInt32 timeoutMS, String& strRemain)
   at System.Data.SQLite.SQLiteCommand.BuildNextCommand()
   at System.Data.SQLite.SQLiteCommand.GetStatement(Int32 index)
   at System.Data.SQLite.SQLiteDataReader.NextResult()
   at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave)
   at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery()
   at ConnectToDb.sqlitecommand.insertToTable(SQLiteConnection sql, String TableName, String Date, String Receipt, String Particulars, String Description, String Category, Double amount) in C:\Users\Jon Stephen\documents\visual studio 2015\Projects\ConnectToDb\ConnectToDb\sqlitecommand.cs:line 45
   at ConnectToDb.Form1.Form1_Load(Object sender, EventArgs e) in C:\Users\Jon Stephen\documents\visual studio 2015\Projects\ConnectToDb\ConnectToDb\Form1.cs:line 36
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Form.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ContainerControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

First sight: The table name for SQL statement should be given from method parameter instead of SQL parameter. Your code should looks like this:

public boolean insertToTable (SQLiteConnection sql, string TableName, string Date, string Receipt, string Particulars, string Description, string Category, double amount)
{
    SQLiteCommand command = new SQLiteCommand(sql);
    if (sql == null)
        return false;
    String statement = 
        "INSERT INTO " + TableName + " (Date, Receipt,Particulars,Description,Category,Amount) " +
        "VALUES (@date, @rec,@part,@desc,@cat,@amt)";

    command.Parameters.AddWithValue("@date",Date);
    command.Parameters.AddWithValue("@rec", Receipt);
    command.Parameters.AddWithValue("@part", Particulars);
    command.Parameters.AddWithValue("@desc", Description);
    command.Parameters.AddWithValue("@cat", Category);
    command.Parameters.AddWithValue("@amt", amount);
    command.Prepare();
    int x = command.ExecuteNonQuery(); // change to command.ExecuteNonQuery() depending what you need
    return true;
}

and while executed:

sqlitecommander.insertToTable(dbConnection, "tester", "4/1", "rec", "part", "desc", "cat", 3.3);

will produce SQL statement as this:

INSERT INTO tester (Date,Receipt,Particulars,Description,Category,Amount) VALUES ('4/1', 'rec', 'part', 'desc', 'cat', 3.3)

The exception occurred since you didn't provide any table name to insert into DB (proving that @table is null or empty), CMIIW.

这是我的sqlite版本的兼容性问题,我下载了32位sqlite而不是64位。我通过Nuget下载了64位然后解决了这个问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM