468,315 Members | 1,498 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,315 developers. It's quick & easy.

Problem binding dataset to a repeater

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_re peater")).DataSource
= tabs_ds;
((Repeater)this.FormView1.Row.FindControl("tabs_re peater")).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_re peater")).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
Oct 10 '06 #1
4 4616
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" <br**@nospam.nospamwrote in message
news:eH****************@TK2MSFTNGP03.phx.gbl...
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_re peater")).DataSource =
tabs_ds;

((Repeater)this.FormView1.Row.FindControl("tabs_re peater")).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_re peater")).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

Oct 10 '06 #2
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_re peater")). 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" <ma******@fitzme.comwrote in message
news:u4**************@TK2MSFTNGP04.phx.gbl...
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" <br**@nospam.nospamwrote in message
news:eH****************@TK2MSFTNGP03.phx.gbl...
>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_r epeater")).DataSource =
tabs_ds;

((Repeater)this.FormView1.Row.FindControl("tabs_r epeater")).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_r epeater")).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


Oct 10 '06 #3
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/lib...trols.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/subscripti...ult.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/subscripti...t/default.aspx.

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

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

Oct 10 '06 #4
if (Request.QueryString["customerid"] != null)
{
In which method do you have this piece of code?
Ideal place when all controls have been loaded -- Page_Load method.
--
Happy Hacking,
Gaurav Vaish | www.mastergaurav.com
www.edujinionline.com
http://eduzine.edujinionline.com
-----------------------------------------
Oct 10 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Stephen Miller | last post: by
2 posts views Thread by Colin Nicholls | last post: by
8 posts views Thread by I am Sam | last post: by
1 post views Thread by Jon S via DotNetMonster.com | last post: by
5 posts views Thread by Brad Baker | last post: by
3 posts views Thread by =?Utf-8?B?dmluY2VudHc1Ng==?= | last post: by
2 posts views Thread by Hong | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.