While loop doesn't work wel...

Jelmer

Regular
Joined
Jan 11, 2006
Messages
96
I've got a while loop that works well if the messagebox is active.
But withouth the messagebox there loses 1 item each time i run this code...

Code:
                int aantal,teller;
                aantal = betalingscondities.Rows.Count;
                teller = 0;                
                while ((aantal-1) > teller) {
                    //MessageBox.Show(betalingscondities.Rows[teller].Cells[1].Value.ToString());
                    stats.add_conditie(betalingscondities.Rows[teller].Cells[1].Value.ToString());
                    Application.DoEvents();
                    teller = teller + 1;
                }

The stats.add:

Code:
        public void add_conditie(string item)
        {
            try
            {
                OleDbConnection Connection = new OleDbConnection();
                Connection = dbase(Connection);
                // Create an OleDb command,  
                OleDbCommand command = new OleDbCommand();
                command.Connection = Connection;
                command.CommandText = "insert into betalingscondities(conditie) values('" + item + "')";                
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }

        }

strange isn't it?
 
Are you sure it works correctly with the Messagebox bit uncommented? Looking at your code (aantal - 1) is the index of the last item. Your while loop iterates whilst teller is lower than the last items index. Therefore the last item is not copied over regardless of whether the Messagbox is shown or not.

Assume aantal was 1, assumably you would want to copy over one item. 1 - 1 however is 0, which is not greater than 0 hence your loop is never entered.
 
Back
Top