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:
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();
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
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].
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!