1

By adding a parameter to the query, the error raised on the DB.Fill(DS) line. The parameter @id is exactly equal to the parameter of the query. I've search a lot but all solution is about typo of parameter name.

SQLiteDataAdapter DB;      
DataSet DS = new DataSet();
DataTable DT = new DataTable();

SetConnection();
sqlCmd = sqlCon.CreateCommand();
string CommandText = "select * from Words where id = @id"; //query;
await sqlCon.OpenAsync();

sqlCmd.Parameters.AddWithValue("@id", 1);
DB = new SQLiteDataAdapter(CommandText, sqlCon);

DS.Reset();
await Task.Run(() => { DB.Fill(DS); });
DT = DS.Tables[0];
sqlCon.Close();
return DT;

UPDATE = Solution As @steve16351 said, the problem is with CommandText. Just add the query to sqlCmd and send it as SQLiteDataAdapter parameter.

sqlCmd = sqlCon.CreateCommand();
sqlCmd.CommandText("select * from Words where id = @id"); //query; <<==  
await sqlCon.OpenAsync();
sqlCmd.Parameters.AddWithValue("@id", 1);
DB = new SQLiteDataAdapter(sqlCmd); <<==
0

3 Answers 3

3

You're not using the SQL command that you've added the parameter to, you're passing the SQLiteDataAdapter the command text directly. Use the overload that takes the command object:

sqlCmd.CommandText = "select * from Words where id = @id"; //query;
sqlCmd.Parameters.AddWithValue("@id", 1);
DB = new SQLiteDataAdapter(sqlCmd);

And here is a more complete example:

using (var conn = new SQLiteConnection(@"Data Source=c:\temp\mydbthatdoesnotexistyet.db;Version=3;"))
{
    conn.Open();

    // create the words table
    var create = conn.CreateCommand();
    create.CommandText = "CREATE TABLE Words (Id INT, Word TEXT)";
    create.ExecuteNonQuery();

    // insert data to the table
    var populate = conn.CreateCommand();
    populate.CommandText = "INSERT INTO Words (Id, Word) VALUES (@Id, @Word)";
    populate.Parameters.AddWithValue("@Id", 1);
    populate.Parameters.AddWithValue("@Word", "abc");
    populate.ExecuteNonQuery();

    // select the data back from the table
    var select = conn.CreateCommand();
    select.CommandText = "SELECT Word FROM Words WHERE Id = @Id";
    select.Parameters.AddWithValue("@Id", 1);

    SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(select);
    DataSet data = new DataSet();
    dataAdapter.Fill(data);
    DataTable table = data.Tables[0];
}
Sign up to request clarification or add additional context in comments.

2 Comments

this error raised: System.NullReferenceException: 'Object reference not set to an instance of an object.' even I have added this line: sqlCmd = new SQLiteCommand();
I've added a complete, working minimal example, perhaps that will help you see where the problem is. Alternatively, add your new code to your question.
0

Like is used for text comparison. You just need a match or not, thus:

string CommandText = "select * from Words where id = @id"; 

1 Comment

Thanks for answering but it's not the answer. It raises the error again.
0

Like :

   string CommandText = "select * from Words where id = $id"; //query;

after :

   sqlCmd.Parameters.AddWithValue("$id", 1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.