?
Solved

asp.net bind repeater control from code behind

Posted on 2014-11-26
8
Medium Priority
?
869 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 2000 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

764 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