user2962142 user2962142 - 20 days ago 5
ASP.NET (C#) Question

how to view data from generic collection to html page in asp.net

Sorry im new to asp.net i wanna view last 6 products in my table in a speicific way, i need each value in its own variable so this is what i did so far :

List<item> listid = new List<item>();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
item product = new item();
product.title = dr["product"].ToString();
product.desc = dr["des"].ToString();
product.price = dr["price"].ToString();
product.owner = dr["owner"].ToString();
listid.Add(product);
}


with an "item" class with all those strings, the question is how do i view each value alone on the .aspx webpage ?

like if i wanted to view the third item price ?

Answer

You have a List of type "item", so you can loop through each item in the list and pull the properties, or get the third item in the list directly by it's index like so:

var singleItem = listid[2]; // This is the third item since list is 0-indexed
Console.WriteLine(singleItem.price); // This should return the item price
Comments