Solved

asp.net bind repeater control from code behind

Posted on 2014-11-26
8
732 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:telliot79
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Legend...

thanks for the help
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

6 Experts available now in Live!

Get 1:1 Help Now