简体   繁体   中英

Looping through SqlDataReader and assigning them to controls of the page (.aspx)

Since I am creating a blog slider, I want it to be dynamic so the latest blogs are retrieved from the database and shown on page. My issue is that I am able to retrieve 3 rows into reader() but unable to assign each row and its values to separate elements like header 'h3',

while(dr.Read())
{
    string h1= (string)dr["Value1"]; 
    string p= (string)dr["Value2"]; 
}

but my main concern is that only one row will be run like this, after that how will I have the remaining rows assigned to controls such as 'h3' and 'p'. The first slider is getting the data from reader but what about the remaining two sliders and it's components such as 'h3' and 'p'

How do I assign the 2nd and 3rd rows to the other controls such as h3 and 'p' ?

Thanks in advance

create a bean class

public class headers
{
   public string head{get;set;}
   public string  p{get;set;}
}

//now change ur code

 var objList = new List<headers>();
   while (dr.Read())
            {
                objDetails = new headers();
                objDetails .head = (string)dr["value1"];
                objDetails .p= (string)dr["value2"];
                objList.Add(objDetails);
            }

now u can do

h1=objList[0].head;
h2=objList[1].head;
h3=objList[2].head;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM