473,394 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 4877
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.