Solved

asp.net bind repeater control from code behind

Posted on 2014-11-26
8
775 Views
Last Modified: 2014-11-26
experts,

I'm making a call to my sql database and attempting to display the results in a repeater control in labels. I want to manipulate the data before I populate the labels. I realize I need to do this in the _ItemDataBound method, but can't figure out how to tie my labels to the data. so far, I have this:

protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Label lblCode = (Label)e.Item.FindControl("lblCode");
         


        }

how do I bind the code from sql select statement to my label?

Thanks in advance
0
Comment
Question by:telliot79
  • 4
  • 4
8 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40466971
Try something like this:
            string strCode = ((DataRowView)(e.Item.DataItem)).Row["code"].ToString();
            // your manipulation here
            lblCode.Text = strCode;

Open in new window

0
 

Author Comment

by:telliot79
ID: 40467090
thanks Robert Schutt.

I'm getting the following error:       object reference not set to an instance of an object.

from this line of code: string strCode = ((DataRowView)(e.Item.DataItem)).Row["code"].ToString();

caused by this:
((DataRowView)(e.Item.DataItem)).Row["code"].ToString()      

'((System.Data.DataRowView)(e.Item.DataItem))' is null

any ideas?
0
 

Author Comment

by:telliot79
ID: 40467124
okay, fixed that, it was looking at the repeater header. I wrapped the code in the following:

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

and now am making progress. I'm now receiving this error:

Unable to cast object of type 'System.Data.Common.DataRecordInternal' to type 'System.Data.DataRowView'.

i'll continue working away
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 40467143
I guess it depends on what is used as data source (I used a DataTable), and the properties of the repeater. If you want I can share the minimal code I used but I guess it would be more valuable to work with your code, can you share more of it?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:telliot79
ID: 40467187
clearly I'm just figuring this out as I go, so thanks for your patience. I'm using a SqlDataReader:

                using (SqlDataReader rdr = cmd.ExecuteReader())
                {
                    try
                    {
                        rpt.DataSource = rdr;
                        rpt.DataBind();
                    }

does that help?
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 40467222
yeah that probably means the type of e.Item.DataItem will be different. I'll see if I can reproduce it. Alternatively you could use a SqlDataAdapter to fill a DataTable and set that as the DataSource but I'm not sure if that would have other effects on your project.
0
 
LVL 35

Assisted Solution

by:Robert Schutt
Robert Schutt earned 500 total points
ID: 40467287
This seems to do the trick with a Data Reader:
string strCode = ((System.Data.Common.DbDataRecord)e.Item.DataItem)["code"].ToString();

Open in new window

0
 

Author Closing Comment

by:telliot79
ID: 40467442
Legend...

thanks for the help
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now