How to combobox select from sqlite database

  • Thread starter Thread starter Booney440
  • Start date Start date
B

Booney440

Guest
I have a winform connected to a sqlite database, the database has 2 tables (DataBase and Teams). I have a combobox when selected need to show whats in the Teams database. I cant figure out how to show. My code is below, what am i doing wrong? I get no errors just a blank combobox.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;
using System.Collections;
using System.Data.SqlClient;

namespace Game_Schedule
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}

// private int i = 0;

private SQLiteConnection sql_con;
private SQLiteCommand sql_cmd;
private SQLiteDataAdapter DB;
private DataSet DS = new DataSet();
private DataTable DT = new DataTable();

private void Form1_Load(object sender, EventArgs e)
{
LoadData();
// fillcomobo();

}


// combobox to select from sub table
protected void FillCombobox()
{
sql_con = new SQLiteConnection
("Data Source=GameDay.db;Version=3;new=False;Compress=True;");
SQLiteConnection conn = new SQLiteConnection(sql_con);
DataSet ds = new DataSet();
try
{
conn.Open();
string CommandText = "select Teams from Team_Names ";
SQLiteDataAdapter da = new SQLiteDataAdapter();
DB = new SQLiteDataAdapter(CommandText, sql_con);
DS.Reset();
da.SelectCommand = sql_cmd;
DB.Fill(DS);
cb_Home.DisplayMember = "Team_Names";
// cb_Home.ValueMember = "ID";
cb_Home.DataSource = ds.Tables[0];
}
catch (Exception ex)
{
//Exception Message
}
finally
{
conn.Close();
conn.Dispose();
}
}





//Set Connection
private void SetConnection()
{
sql_con = new SQLiteConnection
("Data Source=GameDay.db;Version=3;new=False;Compress=True;");
}

//set executequery
private void ExcuteQuery(string txtQuery)
{
SetConnection();
sql_con.Open();
sql_cmd = sql_con.CreateCommand();
sql_cmd.CommandText = txtQuery;
sql_cmd.ExecuteNonQuery();
sql_con.Close();

}

//set loadDB
private void LoadData()
{
SetConnection();
sql_con.Open();
sql_cmd = sql_con.CreateCommand();
string CommandText = "select * from DataBase";
DB = new SQLiteDataAdapter(CommandText, sql_con);
DS.Reset();
DB.Fill(DS);
DT = DS.Tables[0];
dataGridView1.DataSource = DT;
sql_con.Close();
}

#region Team Information

private void teamToolStripMenuItem_Click(object sender, EventArgs e)
{
Teams t = new Teams();
t.Show();
this.Visible = true;
}



private void btn_Save_Click(object sender, EventArgs e)
{

string txtQuery = "insert into DataBase (Date,Time,Home,Away,Location,Win)values('" + tb_Date.Text + "','" +tb_Time.Text + "','" + cb_Home.Text + "', '"+ cb_Away.Text + "', '" + tb_Location.Text + "', '" + tb_Win.Text + "')";
ExcuteQuery(txtQuery);
LoadData();
tb_Home.Clear();
tb_Away.Clear();
// tb_Location.Clear();
// tb_Win.Clear();
}
}
}
#endregion




Booney440

Continue reading...
 
Back
Top