472,780 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 software developers and data experts.

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 4807
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stephen Miller | last post by:
I am using the OnItemDataBound event of Repeater control to nest a DataGrid within the Repeater. When I attempt to bind to the DataGrid using the DataSource method I get the error message "Object...
3
by: Ben Becker | last post by:
I am trying to build a custom crosstab type of grid where I take some items in a data grid and based on the content of the current item compared to the previous item, determine if a new row in a...
2
by: Gastin | last post by:
I am consuming a web serivce from Amazon.Com. I have the following class which was autogenerated by VS.NET when I created a Web Reference to...
2
by: Colin Nicholls | last post by:
Platform: ASP.NET 1.1 I have a repeater nested inside another repeater. My outer repeater is looping fine. I am manually binding the inner repeater to a DataReader obtained from another...
8
by: I am Sam | last post by:
Hi everyone, This problem is making me old. I don't want to get any older. I have a multi-nested repeater control as follows: <asp:Repeater ID="clubRep1" Runat="server">...
1
by: Jon S via DotNetMonster.com | last post by:
Hi all In short, I have a dataset that contains 10 records and the relevant headings. All I want to do is bind this dataset to a asp.net repeater control. All the examples I've seen create...
5
by: Brad Baker | last post by:
I'm trying to write a simple asp.net page which updates some data in a SQL database. At the top of the page I have the following code: <%@ Page Language="C#" Debug="true" %> <%@ import...
3
by: =?Utf-8?B?dmluY2VudHc1Ng==?= | last post by:
I have an ASP.NET 2.0 application. It is pretty basic. What it does is shows a gridview of data from a stored procedure. The user can also select a date filter. The problem is that sometimes an...
2
by: Hong | last post by:
Hi, I have been scratching my head over this problem for hours, the repeater control I am using would only render the controls specified in HeaderTemplate and FooterTemplate, but the repeater...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.