Link to home
Start Free TrialLog in
Avatar of telliot79
telliot79

asked on

asp.net bind repeater control from code behind

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
ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of telliot79
telliot79

ASKER

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?
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
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?
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?
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Legend...

thanks for the help