I have a method which retrieves multiple results from my database, I want to return each of these results as an object back to my page however currently it is just returning the last result of the sql command.
Code on my page calling the method:
Personen persoon1 = Personen.GetOneItem(int.Parse(TextBox_id.Text));
Code of the method:
public static Personen GetAllPersonWithID(int id)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["RestaurantConnection"].Connection
String;
SqlCommand cmd = new SqlCommand("SELECT * FROM Personen where reservering_id=" + id, con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
Personen Item = new Personen();
while (rdr.Read())
{
Item.id = Convert.ToInt32(rdr["id"]);
Item.menu_id = Convert.ToInt32(rdr["menu_id"]);
Item.reservering_id = Convert.ToInt32(rdr["reservering_id"]);
}
con.Close();
return Item;
}
And it only returns the last result in the object.
Thanks in advance
Personen? It sounds as if it's already a collection ofPerson, so you have to fill it in the loop.List<Personen>and would then need to amend thewhile (rdr.Read())content to add to the list for each row.