I'm busy with a task program and then I came on to a problem!
Here is my code:
int id = 1;
List<Button> _bListGroups = new List<Button>();
MySqlCommand mcCommandUserID = new MySqlCommand("SELECT * FROM pakket WHERE gebruikersid=@id", _msConnection);
mcCommandUserID.Parameters.AddWithValue("@id", id);
MySqlDataReader msReader = mcCommandUserID.ExecuteReader();
DataTable dtGetUserid = new DataTable();
dtGetUserid.Load(msReader);
string[] nummer = new string[10];
int i = 0;
foreach (DataRow row in dtGetUserid.Rows)
{
nummer[i] = row["groepsid"].ToString();
i++;
}
MySqlCommand msCommandGetGroup = new MySqlCommand("SELECT * FROM groep WHERE id=@id",_msConnection);
msCommandGetGroup.Parameters.AddWithValue("@id", nummer[i]);
MySqlDataReader msGetGroup = msCommandGetGroup.ExecuteReader();
DataTable dtGetGroup = new DataTable();
dtGetGroup.Load(msGetGroup);
int mtop = 10;
int mleft = 15;
int count = 1;
foreach (DataRow dr in dtGetGroup.Rows)
{
Button temp = new Button();
temp.Name = "bt" + dr["id"];
temp.Text = (string)dr["groepsnaam"];
temp.Width = 125;
temp.Height = 125;
temp.Left = mleft;
temp.Top = mtop;
mleft += 127;
if (count == 2)
{
mtop += 125;
mleft = 15;
count = 0;
}
count++;
_bListGroups.Add(temp);
}
return _bListGroups.ToArray();
When i give my array a specific number(like nummer[1]) i get one button back but if I use the variable i, it doesn't give me any buttons. I already tried to debug it but I couldn't solve it.
Note that you are increasing i
in the end of every iteration. So in the end i
is greater than the last assigned index by 1. Therefore you need i-1
:
msCommandGetGroup.Parameters.AddWithValue("@id", nummer[i-1]);
Also you might want to have some prechecks here for edge cases, say when there is no rows in dtGetUserid.Rows
.