Click here to Skip to main content
15,887,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i create a winform program that load the data from ms access database to my listbox. when i click my item which i want to update on database and datasource on my listbox and click the update button, the item just wont update for the first time clicked but the second does(on the same item), so everytime i need to modify my data i need to choose my item and click the update button two times. that's is crazy lol..

C#
private void Fill()
        {
            string strSQL = "SELECT * FROM Item ORDER BY ITEM";
            OleDbDataAdapter myCmd = new OleDbDataAdapter(strSQL, GetConnection());
            DataSet dtSet = new DataSet();
            myCmd.Fill(dtSet, "Item");
            DataTable dTable = dtSet.Tables[0];
            foreach (DataRow dtRow in dTable.Rows)
            {
                _productlist.Add(new PRODUCTLIST() { ID = dtRow["ID"].ToString(), ITEM = dtRow["ITEM"].ToString(), ITEM_DESC = dtRow["ITEM_DESC"].ToString() });
            }
            listBox1.DisplayMember = "ITEM";
            listBox1.DataSource = _productlist;
            listBox1.ValueMember = "ID";
            GetConnection().Close();
        }

    private void Form1_Load(object sender, EventArgs e)
        {
            // make ID textbox un editable
            textBoxID.Enabled = false;

            // fill the listbox
            Fill();

            textBoxID.DataBindings.Add("Text", _productlist, "ID", false, DataSourceUpdateMode.Never);
            textBoxITEM.DataBindings.Add("Text", _productlist, "ITEM", false, DataSourceUpdateMode.Never);
            textBoxITEMDESC.DataBindings.Add("Text", _productlist, "ITEM_DESC", false, DataSourceUpdateMode.Never);
        }

private void buttonUpdate_Click(object sender, EventArgs e)
        {
            //update the database
            OleDbDataAdapter cmd = new OleDbDataAdapter();
            cmd.UpdateCommand = new OleDbCommand("UPDATE Item SET ITEM = @ITEM, ITEM_DESC = @ITEM_DESC WHERE ID = @ID",GetConnection());
            cmd.UpdateCommand.Parameters.AddWithValue("@ITEM", textBoxITEM.Text);
            cmd.UpdateCommand.Parameters.AddWithValue("@ITEM_DESC", textBoxITEMDESC.Text);
            cmd.UpdateCommand.Parameters.AddWithValue("@ID", Convert.ToInt32(textBoxID.Text));
            cmd.UpdateCommand.ExecuteNonQuery();

            // update the datasource
            _productlist.Clear();
            listBox1.DataSource = null;
            listBox1.Items.Clear();
            Fill();
            listBox1.Update();
        }



basic thing that happen:

1. load data to listbox with databind to textbox
2. click item on my listbox and modify data from textbox
3. click the update button to update database and datasource
4. datasource not update for the first time but database does update
5. repeat no.2
6. repeat no.3
7. datasource updated

what i want:

1. load data to listbox with databind to textbox
2. click item on my listbox and modify data from textbox
3. click the update button to update database and datasource
4. datasource and database updated

how do i fix this bug on my application?? please let me know if i need to be more clear..
Posted
Updated 16-Mar-13 7:25am
v2

Once the values are updated in the databases.
Try to retrieve and bind the data again with Listbox.
 
Share this answer
 
Comments
zulfakar 16-Mar-13 13:19pm    
yes..i already do that on my button click event above with Fill(); which is this..

private void Fill()
{
string strSQL = "SELECT * FROM Item ORDER BY ITEM";
OleDbDataAdapter myCmd = new OleDbDataAdapter(strSQL, GetConnection());
DataSet dtSet = new DataSet();
myCmd.Fill(dtSet, "Item");
DataTable dTable = dtSet.Tables[0];
foreach (DataRow dtRow in dTable.Rows)
{
_productlist.Add(new PRODUCTLIST() { ID = dtRow["ID"].ToString(), ITEM = dtRow["ITEM"].ToString(), ITEM_DESC = dtRow["ITEM_DESC"].ToString() });
}
listBox1.DisplayMember = "ITEM";
listBox1.DataSource = _productlist;
listBox1.ValueMember = "ID";
GetConnection().Close();
}
this is what i use to update the datasource

// clear the item source
_productlist.Clear();
// set datasource to null
listBox1.DataSource = null;
// wait for half second for database to updated >.<<br mode="hold" />System.Threading.Thread.Sleep(500);
// re-populate the listbox after half second
Fill();
 
Share this answer
 

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