Problem binding dataset to a repeater

B

Brad Baker

I'm going a little crazy :) I'm trying to bind a repeater control to a
dataset on page load using the following code:

if (Request.QueryString["customerid"] != null)
{
string customerid = Request.QueryString["customerid"];

//open connection
SqlConnection m_conn = new SqlConnection("Server=server;
Database=database; UId=username; Pwd=password");
m_conn.Open();

// Create a SQL query
SqlDataAdapter tabs_query = new SqlDataAdapter("select site_id
from table where customer_id=@customerid", m_conn);

tabs_query.SelectCommand.Parameters.Add(new
SqlParameter("@customerid", customerid));

// Create and fill a DataSet
DataSet tabs_ds = new DataSet();
tabs_query.Fill(tabs_ds);

// Temp code to see if the datasource is being populated
if (tabs_ds != null) {
Response.Write("Results table is not null");
} else {
// write some kind of error message
Response.Write("Results table is null");
}

// (The code above produces not null)

// Bind tabs_repeater to the DataSet.
((Repeater)this.FormView1.Row.FindControl("tabs_repeater")).DataSource
= tabs_ds;
((Repeater)this.FormView1.Row.FindControl("tabs_repeater")).DataBind();

m_conn.Close();
}

.... later in the page .....

<ASP:Repeater id="tabs_repeater" runat="server">
<HeaderTemplate>
<table><tr>
</HeaderTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "site_id") %>
</ItemTemplate>
<FooterTemplate>
<td><h2>Information</h2></td></tr></table>
</FooterTemplate>
</ASP:Repeater>


I'm getting an error indicating: Object reference not set to an instance of
an object. The error references this line:
((Repeater)this.FormView1.Row.FindControl("tabs_repeater")).DataSource =
tabs_ds;

I'm probably doing something stupid but I just can't seem to figure out what
it is. Can anyone help?

Thank You!
Brad
 
M

Mark Fitzpatrick

It's not the binding that's really the issue, it's the location of the
repeater. If you're getting an object is null error, the findcontrol is
returning null itself so there's nothing to bind to. If it was returning an
object of the wrong type you would get a conversion error. Try looking at
the trace to see exactly where in the control hierarchy the repeater is. You
could also write some for loops to loop through a particular control's
subscontrols as a good way to test if the repeater is there. You just need
to find the correct parent to use the findcontrols method on in order to
find it, then you should be able to bind without any problems.
 
B

Brad Baker

Mark -

Thanks - that gives me some idea as to where to direct my efforts. I've
enabled tracing but I really couldn't find anything that looked useful (at
least a newbie like me). I'm trying to think through how I can loop through
the controls right now.

I actually have my repeater inside of a formview (formview1)
"edititemtemplate":

<asp:FormView ID="FormView1" runat="server" DataSourceID="Datasource"
onmodechanged="FormView1_ModeChanged"
OnPageIndexChanging="FormView1_PageIndexChanging" Width="100%"
DefaultMode="ReadOnly">
<EditItemTemplate>
<table class="tableborder">
<tr><td colspan="4">
<ASP:Repeater id="tabs_repeater" runat="server">
<HeaderTemplate>
<table><tr>
</HeaderTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "site_id") %>
</ItemTemplate>
<FooterTemplate>
<td><h2>Customer Configuration</h2></td></tr></table>
</FooterTemplate>
</ASP:Repeater>
</td>
</tr>
.....

I've found other controls using the code
((Repeater)this.FormView1.Row.FindControl("tabs_repeater")). So I'm
somewhat stumped as to why its breaking with the repeater control.

I'll keep plugging away at this - hopefully I can sort it out. If you have
any other suggestions or ideas please let me know. :)

Thanks so much,
Brad


Mark Fitzpatrick said:
It's not the binding that's really the issue, it's the location of the
repeater. If you're getting an object is null error, the findcontrol is
returning null itself so there's nothing to bind to. If it was returning
an object of the wrong type you would get a conversion error. Try looking
at the trace to see exactly where in the control hierarchy the repeater
is. You could also write some for loops to loop through a particular
control's subscontrols as a good way to test if the repeater is there. You
just need to find the correct parent to use the findcontrols method on in
order to find it, then you should be able to bind without any problems.

--
Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

Brad Baker said:
I'm going a little crazy :) I'm trying to bind a repeater control to a
dataset on page load using the following code:

if (Request.QueryString["customerid"] != null)
{
string customerid = Request.QueryString["customerid"];

//open connection
SqlConnection m_conn = new SqlConnection("Server=server;
Database=database; UId=username; Pwd=password");
m_conn.Open();

// Create a SQL query
SqlDataAdapter tabs_query = new SqlDataAdapter("select site_id
from table where customer_id=@customerid", m_conn);

tabs_query.SelectCommand.Parameters.Add(new
SqlParameter("@customerid", customerid));

// Create and fill a DataSet
DataSet tabs_ds = new DataSet();
tabs_query.Fill(tabs_ds);

// Temp code to see if the datasource is being populated
if (tabs_ds != null) {
Response.Write("Results table is not null");
} else {
// write some kind of error message
Response.Write("Results table is null");
}

// (The code above produces not null)

// Bind tabs_repeater to the DataSet.

((Repeater)this.FormView1.Row.FindControl("tabs_repeater")).DataSource =
tabs_ds;

((Repeater)this.FormView1.Row.FindControl("tabs_repeater")).DataBind();

m_conn.Close();
}

... later in the page .....

<ASP:Repeater id="tabs_repeater" runat="server">
<HeaderTemplate>
<table><tr>
</HeaderTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "site_id") %>
</ItemTemplate>
<FooterTemplate>
<td><h2>Information</h2></td></tr></table>
</FooterTemplate>
</ASP:Repeater>


I'm getting an error indicating: Object reference not set to an instance
of an object. The error references this line:
((Repeater)this.FormView1.Row.FindControl("tabs_repeater")).DataSource =
tabs_ds;

I'm probably doing something stupid but I just can't seem to figure out
what it is. Can anyone help?

Thank You!
Brad
 
S

Steven Cheng[MSFT]

Hello Brad,

From the code snippet you provided, your Findcontrol code should be ok, the
problem here is likely due to the place where you call the FindControl and
databinding code. Actually, you put the Repeater control in the FormView's
EditTemplate, that means only when the FormView is in edit mode will you be
able to access the Repeater control(or any other sub controls in the edit
template).

Also, since this databinding customization code logic is more specific to
the FormView control, it is better to put them in FormView's event handler
and the "ItemCreated" event is a good place to handle our customization on
the FormView's Row or its child controls.

#FormView.ItemCreated Event
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.
itemcreated.aspx


Here is my modified test FormView and code behind code, you can test them
on your side for reference:

============FormView template=============

<asp:FormView ID="FormView1" runat="server"
DataKeyNames="CategoryID" DataSourceID="SqlDataSource1"
OnItemCreated="FormView1_ItemCreated">
<EditItemTemplate>
CategoryID:
<asp:Label ID="CategoryIDLabel1" runat="server" Text='<%#
Eval("CategoryID") %>'></asp:Label><br />
CategoryName:
<asp:TextBox ID="CategoryNameTextBox" runat="server"
Text='<%# Bind("CategoryName") %>'></asp:TextBox><br />
<br /><hr /><br />
<table class="tableborder">
<tr>
<td colspan="4">
<asp:Repeater ID="tabs_repeater" runat="server">
<HeaderTemplate>
<table>
<tr>
</HeaderTemplate>
<ItemTemplate>
<%# Container.DataItem %>
</ItemTemplate>
<FooterTemplate>
<td>
<h2>
Customer Configuration</h2>
</td>
</tr></table>
</FooterTemplate>
</asp:Repeater>
</td>
</tr>
</table>
<br />
<asp:LinkButton ID="UpdateButton" runat="server"
CausesValidation="True" CommandName="Update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
...............................
</InsertItemTemplate>
<ItemTemplate>
.........................
</ItemTemplate>
</asp:FormView>
========================================

In "ItemCreaetd" event, we do the databinding for the repeater control:
========code behind event handler==========
protected void FormView1_ItemCreated(object sender, EventArgs e)
{
if (FormView1.CurrentMode == FormViewMode.Edit)
{
string[] items = new string[] { "aaa","bbb","ccc","ddd"};

Repeater rpt = FormView1.Row.FindControl("tabs_repeater") as
Repeater;

if (rpt != null)
{
rpt.DataSource = items;
rpt.DataBind();
}
}
}

=============================

Please feel free to let me know if you have anything unclear or any further
questions on this.


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top