Solved

asp.net bind repeater control from code behind

Posted on 2014-11-26
8
789 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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
 

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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