I am trying to loop multiple comments in two labels. For some reason, only the latest comment is being displayed in the label while when I output it using repsonse.write; all the comments display. I am pretty new to .NET and can't figure out what I am doing wrong. Any help would be great. Thanks.
<asp:Label ID="lblID" runat="server"></asp:Label>
<asp:Label ID="lblComment" runat="server"></asp:Label>
Dim dt As DataTable
Dim dr As DataRow
dt = ds.Tables(0)
For Each dr In dt.Rows
lblID.Text = dr("placementId").ToString()
lblComment.Text = dr("comment").ToString()
Quite simple actually, you are overwriting the text stored in the labels each time you loop through a comment as you are using 'lblID.Text =' and 'lblComment.Text'.
You should look into use a Repeater <asp:repeater>. Then you can bind your DataTable to the repeater. Let me know if you need help getting the repeater to work, and I'll be glad to assist. There should be many examples on the internet
Solved it using this
lblID.Text += dr("placementId").ToString()
That is definitely one way to solve it. There are numerous reasons why I wouldn't approach it that way, and would go the router of a repeater, but nonetheless, good thing that you found an answer. I do strongly recommend researching repeaters (they are fantastic for these types of problems and make your coding very minimal, you usually just had it your datasource, bind it and in your aspx page you told it how to layout the information using an <itemtemplate>.
Thank you for the reply and suggestion. I will definitely have a look when I can but at the moment I need to get things done and I am an absolute newbie in the whole .NET development and the site I am making has not security implications or even performance issues as it will be used by maybe 100-200 people at most. The code will still be looked by senior developers (who suggested to do that for now). But I will surely try to find how to do it using repeater. Thank you for your time.
If those senior devs suggested that or greenlight it to go to production they probably should not be senior devs.
Haha probably. Or probably they can't give much damn about a project they seem unwilling to take on. Otherwise they probably won't have left it for an absolute newbie in .NET environment to go throug a full life cycle development for it ;p. It's really an awkward situation for me. But I did found out how to use repeater for it now.
Thank you for being braver than I. I thought this the moment I read Niraj Pandey's response, but wasn't brave enough to say it. I personally would not greenlight the use the += over a repeater in this situation.
Glad to hear it
This topic is now archived. It is frozen and cannot be changed in any way.