I am currently learning how to use database connections and how to read and write data. I am using Mysql data/connector 6.1 for visual studio. This supports access to a localhost mysql server.
I want to display personal information of the person logged in for which i use this code:
string query = "SELECT * FROM gebruiker WHERE id='" + id + "'";
List<string>[] list = new List<string>[5];
list[0] = new List<string>();
list[1] = new List<string>();
list[2] = new List<string>();
list[3] = new List<string>();
list[4] = new List<string>();
if (this.openConnection() == true)
{
MySqlCommand cmd = new MySqlCommand(query, connection);
//Een nieuw datareader object maken en dan query uitvoeren
MySqlDataReader dataReader = cmd.ExecuteReader();
while (dataReader.Read())
{
list[0].Add(dataReader["id"].ToString());
list[1].Add(dataReader["voornaam"].ToString());
list[2].Add(dataReader["achternaam"].ToString());
list[3].Add(dataReader["geboortedatum"].ToString());
list[4].Add(dataReader["Rol_id"].ToString());
}
dataReader.Close();
this.closeConnection();
return list;
}
I use Lists because I am following this tutorial: tutorial
Then when i return the list i want to get that data and add it to labels on my winform. this is the code i use:
DBconnect connectDB = new DBconnect();
List<string>[] persGegevens = connectDB.persoonlijkeGegevens(userId);
for (int i = 0; i <= persGegevens.Count(); i++)
{
switch (i)
{
case 0:
break;
case 1:
lblVoornaamVrbl.Text = persGegevens[i].ToString();
break;
case 2:
lblAchternaamVrbl.Text = persGegevens[i].ToString();
break;
case 3:
lblGbrtedatumVrbl.Text = persGegevens[i].ToString();
break;
case 4:
lblFuntieVrbl.Text = persGegevens[i].ToString();
break;
default:
break;
}
}
But unfortunatly the code does not convert the data in List toString but the element will get converted toString. This is what i get: system.collections.generic.list`1[system.string] I also tried to do this with a foreach but that did not work because i can't specify which element has to be placed in what label.
Do you guys know what is the problem here? and is this the right way to retrieve data? or should i use something else to store my data?
iddirectly to the string. Take a look here for more info