Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a very simple project in Visual Studio 2022 using Winforms C# SQLite, that includes a combobox that selects from sqlite database.

Based on the selection in the combobox, the datagridview is filled with data from another table.

When my LoadData() starts, after I make my selection from my combobox, it generates an error System.Data.SQLite.SQLiteException: "unknown error Insufficient parameters supplied to the command' and it is referencing a line that says:

adapter.Fill(ds, "Mileage");

C#
using System;
using System.Data;
using System.Data.SQLite;
using System.Windows.Forms;

namespace ArukahHouse
{
    public partial class MileageTrack : Form
    {
        
        SQLiteConnection sqlite_conn;
        SQLiteCommand sqlite_cmd;
        SQLiteDataReader sqlite_datareader;
        string query;
        SQLiteDataAdapter adapter;
        BindingSource bsource = new BindingSource();
        DataSet ds = null;
        

        public MileageTrack()
        {
            InitializeComponent();
        }

        private void MileageTrack_Load(object sender, EventArgs e)
        {
            /*sqlite_conn = new SQLiteConnection("Data Source=Arukah.db;Foreign Keys=True;");
            sqlite_cmd = new SQLiteCommand();
            sqlite_conn.Open();
            sqlite_cmd.Connection = sqlite_conn;
            sqlite_cmd.CommandText = "SELECT * FROM Car";
            sqlite_datareader = sqlite_cmd.ExecuteReader();
            while (sqlite_datareader.Read())
            {
                autoidcomboBx.Items.Add(sqlite_datareader["car_ID"]);
                autoidcomboBx.Items.Add(sqlite_datareader["car_Make"]);
            }
            sqlite_conn.Close(); */

            sqlite_conn = new SQLiteConnection("Data Source=Arukah.db;Foreign Keys=True;");
            query = "SELECT * FROM Car";
            sqlite_cmd = new SQLiteCommand(query,sqlite_conn);
            sqlite_conn.Open();
            sqlite_cmd.Connection = sqlite_conn;
            sqlite_datareader = sqlite_cmd.ExecuteReader();
            while (sqlite_datareader.Read())
            {
                query=sqlite_datareader[0].ToString()+" "+ sqlite_datareader[1].ToString()+" "+ sqlite_datareader[2].ToString() + " " + sqlite_datareader[3].ToString();
                autoidcomboBx.Items.Add(query);
            }
            sqlite_datareader.Close();
        }
        private void autoidcomboBx_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData(); 
        }

        private void LoadData()
        {
            sqlite_conn = new SQLiteConnection("Data Source=Arukah.db;Foreign Keys=True;");
            query = "SELECT mile_Date, begin_Miles, end_Miles FROM Mileage WHERE car_ID=@car_ID";
            sqlite_cmd.Parameters.Add("@car_ID", System.Data.DbType.Int16).Value = autoidcomboBx;
            sqlite_cmd.ExecuteNonQuery();
            adapter = new SQLiteDataAdapter(query, sqlite_conn);
            sqlite_conn.Open();
            ds = new DataSet();
            SQLiteCommandBuilder builder = new SQLiteCommandBuilder(adapter);
            adapter.Fill(ds, "Mileage");
            bsource.DataSource = ds.Tables["Mileage"];
            dataGridView1.DataSource = bsource;
            sqlite_conn.Close();
        }
    }
}


What I have tried:

I have tried every resource I could think of by searching the web, but I am pretty new to everything and I can't figure it out.
Posted
Updated 14-Feb-22 5:26am
Comments
PIEBALDconsult 11-Feb-22 17:01pm    
Make sure the parameter value isn't null.
Blue Ocean 11-Feb-22 20:01pm    
I think the parameter is a problem, yes. If I
//WHERE car_ID=@car_ID";
//sqlite_cmd.Parameters.Add("@car_ID", System.Data.DbType.Int16).Value = //autoidcomboBx.SelectedIndex;
and the if () solution below with the autoidcombobx.SelectIndex change from below, I can get data to populate the datagrid, but only if dont include the mile_Date in the query. If I include it, another error is thrown: System.FormatException
HResult=0x80131537
Message=String was not recognized as a valid DateTime.

That's correct, you are providing Insufficient parameters, check the below line you are providing control name as @Car_ID parameter not the selected value:
sqlite_cmd.Parameters.Add("@car_ID", System.Data.DbType.Int16).Value = autoidcomboBx;
Change you code accordingly.
private void LoadData()
{
	if (autoidcomboBx.SelectedIndex > 0)
	{
		sqlite_conn = new SQLiteConnection("Data Source=Arukah.db;Foreign Keys=True;");
        query = "SELECT mile_Date, begin_Miles, end_Miles FROM Mileage WHERE car_ID=@car_ID";
        sqlite_cmd.Parameters.Add("@car_ID", System.Data.DbType.Int16).Value = autoidcomboBx.SelectedIndex;
		// ......
		// Other code
	}
}
 
Share this answer
 
Comments
Blue Ocean 11-Feb-22 20:13pm    
Thank You Imran, this was part of the problem. Please see my comment to PIEBALDconsult, there are apparently quite a few more issues. There seems to be a problem with the WHERE part of statement, the Parameters, and also the mile_Date format of my sqlite table below:
auto_ID INTEGER PRIMARY KEY AUTOINCREMENT
UNIQUE
NOT NULL,
mile_Date DATE NOT NULL,
begin_Miles INTEGER NOT NULL,
end_Miles INTEGER NOT NULL,
car_ID INTEGER REFERENCES Car (car_ID)

Also, is SelectedIndex correct? or should it be something else. If I select a value in the combobx and it populates the datagridview then go back to combobx and select another value it does nothing.
C#
qlite_cmd.Parameters.Add("@car_ID", System.Data.DbType.Int16).Value = autoidcomboBx;

A reference to a ComboBox cannot be a valid identifier. You need to find the actual Car_ID value for the item selected by the user, and pass that to your query.
 
Share this answer
 
For all who are interested in the final solution that spanned two different questions, go here:

https://www.codeproject.com/Questions/5325114/How-to-concatenate-a-multiple-field-combobox-selec
 
Share this answer
 
OK, with everyone's guidance I seem to have worked most things out now. I still have one issue that evades me.

Here is my cleaned up code that seems to work great so far, with the one exception:

C#
using System;
using System.Data;
using System.Data.SQLite;
using System.Windows.Forms;

namespace ArukahHouse
{
    public partial class MileageTrack : Form
    {
        SQLiteConnection sqlite_conn;
        SQLiteCommand sqlite_cmd;
        SQLiteDataReader sqlite_datareader;
        string query;
        SQLiteDataAdapter adapter;
        BindingSource bsource = new BindingSource();
        DataSet ds = null;

        public MileageTrack()
        {
            InitializeComponent();
        }

        private void MileageTrack_Load(object sender, EventArgs e)
        {
            sqlite_conn = new SQLiteConnection("Data Source=Arukah.db;Foreign Keys=True;");
            query = "SELECT car_ID, car_Make, car_Model, car_Year FROM Car";
            sqlite_cmd = new SQLiteCommand(query,sqlite_conn);
            sqlite_conn.Open();
            sqlite_cmd.Connection = sqlite_conn;
            sqlite_datareader = sqlite_cmd.ExecuteReader();
            while (sqlite_datareader.Read())
            {
                query = sqlite_datareader[0].ToString();// +" "+ sqlite_datareader[1].ToString()+" "+ sqlite_datareader[2].ToString() + " " + sqlite_datareader[3].ToString();
                autoidcomboBx.Items.Add(query);
            }
            sqlite_datareader.Close();
        }

        private void autoidcomboBx_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData(); 
        }

        private void LoadData()
        {
            if (autoidcomboBx.SelectedIndex >= 0)
            {
                sqlite_conn = new SQLiteConnection("Data Source=Arukah.db;Foreign Keys=True;");
                query = "SELECT STRFTIME(mile_Date) AS MileDate, begin_Miles, end_Miles FROM Mileage WHERE car_ID="+ autoidcomboBx.SelectedItem;
                adapter = new SQLiteDataAdapter(query, sqlite_conn);
                sqlite_conn.Open();
                ds = new DataSet();
                SQLiteCommandBuilder builder = new SQLiteCommandBuilder(adapter);
                adapter.Fill(ds, "Mileage");
                bsource.DataSource = ds.Tables["Mileage"];
                dataGridView1.DataSource = bsource;
                sqlite_conn.Close();
            }
        }
    }
}


The problem lies in the datareader strings being passed to the combobox. If I only use
C#
query = sqlite_datareader[0].ToString();
everything works excellent. The value passed to the combobox is the car_ID which is either:
1
2
3

If I use the following, I get an exception error and it highlights the value of the string index [1].
C#
query = sqlite_datareader[0].ToString()+" "+ sqlite_datareader[1].ToString()+" "+ sqlite_datareader[2].ToString() + " " + sqlite_datareader[3].ToString();

System.Data.SQLite.SQLiteException: 'SQL logic error<br />
near "CHEVROLET": syntax error'   
Which is the value of sqlite_datareader[1].ToString that I select in the combobox. The combobox loads fine with these values:
1 FORD F-150 2018
2 DODGE RAM 2021
3 CHEVROLET EQUINOX 2017

the error occurs on selection LoadData().

Any thoughts? Many thanks!
 
Share this answer
 
Comments
Richard MacCutchan 14-Feb-22 12:00pm    
In this case your query will be:
Copy Code
SELECT STRFTIME(mile_Date) AS MileDate, begin_Miles, end_Miles FROM Mileage WHERE car_ID=3 CHEVROLET EQUINOX 2017;

which is not a valid statement because of the space between "3" and "CHEVROLET EQUINOX 2017". Strings with spaces need to be in quotes, so your query should be:
Copy Code
query = "SELECT STRFTIME(mile_Date) AS MileDate, begin_Miles, end_Miles FROM Mileage WHERE car_ID='"+ autoidcomboBx.SelectedItem + "'"; // add single quotes round selector key.


However this will most likely not work, as car_ID is the first column only, and not as concatenation of all columns.
Blue Ocean 14-Feb-22 13:07pm    
I should have caught the quotes issue, you are right it doesnt return the records but it doesnt generate the error.
Richard Deeming 14-Feb-22 12:54pm    
This is not a "solution" to your question. Either click the green "Improve question" link and update your question, or mark this one as "solved" and ask a new question.

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