Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I got the error "Insufficient parameters supplied to the command" on the line I comment /**********/. I don't know what's wrong with it? Please help me?

C#
string _search;
private void txtSearchBox_TextChanged(object sender, TextChangedEventArgs e)
{
    _search = txtSearchBox.Text;
}

SQLiteConnection sqlite_conn = new SQLiteConnection("Data Source = music.db");
SQLiteCommand sqlite_cmd;

private void btn_search_Click(object sender, RoutedEventArgs e)
{
    sqlite_conn.Open();
    sqlite_cmd = sqlite_conn.CreateCommand();
    sqlite_cmd.CommandText = "select Title, ArtistName, AlbumName from Song,Artist,Album where (Song.Title=@title) and (Song.AlbumID=Album.AlbumID) and (Song.ArtistID=Artist.ArtistID)";
    
    sqlite_cmd.Parameters.Add("@title", DbType.String, -1);
    sqlite_cmd.Parameters["@title"].Value = _search;
    
    DataSet dataSet = new DataSet();
    SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(sqlite_cmd.CommandText, sqlite_conn);
    
    dataAdapter.Fill(dataSet); /*****************/
    datagrid.ItemsSource = dataSet.Tables[0].DefaultView;
    datagrid.CanUserAddRows = false;
    datagrid.CanUserDeleteRows = false;
    sqlite_conn.Close();
}
Posted
Comments
Kevin Marois 31-Dec-15 15:02pm    
The command looks funny. It appears you're trying to do a join. Are you sure it works? Have you tried it in SQLiteManager?
Member 12035413 31-Dec-15 15:24pm    
Yes, the query has been done in SQLiteManager.
ZurdoDev 31-Dec-15 15:25pm    
1. The error is probably because _search is null. However, this is very easy for you to debug and find out.
2. Use joins instead of whatever that old fashioned way of where clausing is called.
3. I suggest using cmd.Parameters.AddWithValue("@title", _search) instead. It's cleaner and the way your are doing it is normally not necessary.

1 solution

The error is occurring because you are constructing the SQLiteDataAdaptor with the CommandText, rather than the SQLiteCommand object. The CommandText does not contain the SQLiteParameter object - just its placeholder in the string.

Changing
C#
SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(sqlite_cmd.CommandText, sqlite_conn);

to
C#
SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(sqlite_cmd, sqlite_conn);

should fix the issue.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900