So i have got a form in which there are two sections one to add and one to delete.
But the problem is when i add data and after which when i check the combobox associated with delete section i can see all previous data except the one which i just added. So i need some kind of solution to refresh and re get everything into combobox as soon as i click Add button but no luck the latest data wont show.
Code for add button:
private void btnAddSubj_Click(object sender, EventArgs e)
{
OleDbDataReader cmd = ad.select("SELECT TOP 1 ID FROM subjects WHERE ID = " + int.Parse(txtSubjID.Text));
if (cmd.Read())
{
MessageBox.Show("Subject ID you entered is taken, please select a different one");
}
else
{
ad.insert("insert into subjects (`ID`,`subjectName`) values(" + int.Parse(txtSubjID.Text) + ",'" + txtSubjName.Text + "')");
}
populateComboBoxSubjName();
}
Here ad is associated to class which i created and contains all the methods for insert,select,delete,update.
code for populateComboBoxSubjName:
private void populateComboBoxSubjName()
{
comboBoxSubjName.Items.Clear();
OleDbDataReader cmd = ad.select("SELECT * FROM subjects");
while (cmd.Read())
{
for (int f = 0; f < cmd.FieldCount; f+=2)
{
string data = (cmd.GetValue(f).ToString() + "-" + cmd.GetValue(f + 1).ToString());
comboBoxSubjName.Items.Add(data);
}
}
}
Button Delete Code:
private void btnDeleteSubj_Click(object sender, EventArgs e)
{
string selected = this.comboBoxSubjName.GetItemText(this.comboBoxSubjName.SelectedItem);
string[] idToDelete = selected.Split('-');
ad.delete("DELETE FROM subjects WHERE ID=" + int.Parse(idToDelete[0]));
}
I was trying to perform sql queries using another class (i created one and coded all the queries and connections with different methods for each like insert,delete,select) so i wouldnt need to write whole command again and again but for some reason it didnt work. I had to write every command again and again and that worked perfectyl.