My Project Details: Visual Studio WinForms C# SQLite; a simple form to retrieve mileage records from the autos selected.
I have a combobox populated with the following from query:
query = sqlite_datareader[0].ToString()+" "+ sqlite_datareader[1].ToString()+" "+ sqlite_datareader[2].ToString() + " " + sqlite_datareader[3].ToString();
The actual names and values are:
car_ID car_Make car_Model car_year
so the combo box has the following records:
1 FORD F-150 2018
2 DODGE RAM 2021
3 CHEVROLET EQUINOX 2017
Based on the selection of one of these three in the combobox, the following query is run:
query = "SELECT STRFTIME(mile_Date) AS MileDate, begin_Miles, end_Miles FROM Mileage WHERE (car_ID='"+ autoidcomboBx.SelectedItem +"')";
No error occurs, but it doesn't pull the records I want because it still sees a string and not four values separated by spaces where I want the first value to compare to
car_ID
.
Below is the full code for reference:
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();
}
}
}
}
What I have tried:
Hours of online searches. I thought about
concat_ws
, but I can't seem to make that work.