jeudi 9 juillet 2015

Foreach keeps looping

So i have this project that needs to print data from the database. I do this with a simple foreach loop:

public void LoadDatabase()
    {
        _connection.Open();
        _dataAdapter.Fill(_dataTable);

        try
        {
            foreach (DataRow row in _dataTable.Rows)
            {
                Program.AnimalInfo.Info_ID_ListBox.Items.Add(row["Animal_ID"].ToString());
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Failed to LoadDatabase()" + ex.Message);
        }
        _connection.Close();     
    }

The problem is that it will loop anywhere from 2 times to 6 times meaning that it prints everything at least twice. So at the moment my database contains Animal_Id's id:1 and id:2. Now i get in my listbox (1, 2, 1, 2) or more depending on the amount of loops. I have no idea why this is happening and how to fix this. So all help would be highly appreciated

ps: if more code or information is needed please let me know.

pss: this is for a windows mobile 6.5 device with .net 3.5 build in Visual studio 2008. also im useing sqlite (not the newest version)

Aucun commentaire:

Enregistrer un commentaire