How to get access to the current row number being output from database.

  • Thread starter Thread starter Justin
  • Start date Start date
J

Justin

I'm a newby to C#, but in ColdFusion I've used <cfif #currentrow# Mod 2 is 1>.
Here is my C# code that's looping through the data.
Do I have to write my own counter or is there some way to access the row number?

while (objDataReader.Read() == true)
{
strResultsHolder += "<li><a href='customer_info.aspx?ID=";
strResultsHolder += objDataReader["AID"];
strResultsHolder += "'>";
strResultsHolder += objDataReader["Customer_Name"];
strResultsHolder += "</a></li>";
}

Thanks,

Justin
 
I ended up using the code below.
I looked at the repeater, but didn't see how it would give me the
flexability I wanted (to list 10 items in each cell).

int i = 0;

while (objDataReader.Read() == true)
{
strResultsHolder += "<li><a href='customer_info.aspx?ID=";
strResultsHolder += objDataReader["AID"];
strResultsHolder += "'>";
strResultsHolder += objDataReader["Customer_Name"];
strResultsHolder += "</a></li>";

if ( i % 10 == 9 )
{
strResultsHolder += "</td><td valign=top>";
}

i++;
}

Thanks,

Justin

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Hello

Here is how you can do it with a repeater

<asp:Repeater runat="server" id="myRep">
<itemtemplate>
<li>
<a href='customer_info.aspx?Id=<%# DataBinder.Eval(Container.DataItem,
"AID")%>'>
<%# DataBinder.Eval(Container.DataItem, "AID")%>
</a>
</li>
<asp:placeholder runat="server" visible="<%# Container.ItemIndex % 10 ==
9%>">
</td><td valign="top">
</asp:placeholder>
</itemtemplate>
</asp:repeater>


You may also want to look at DataList, it can't do what you want, but it is
close. If you still want to user string concatenation, then use
StringBuilder, which would have a better performance.

Best regards,
Sherif
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top