Question

I've been tasked with something somewhat unique and I haven't found a similar example in my research. I have an ascx, and in that ascx I'm registering a custom control.

The ascx opens a SQL connection, executes a reader - and then passes that reader to the custom control which has a repeater in it.

The custom control is loaded, however I'm getting an "Object reference not set to an instance of an object" error on the repeater. I've tried all the normal things (and some not so normal) in an attempt to bind. Here's the outline (some things have been omitted for brevity)

The main ascx:

<%@ Control Language="c#" AutoEventWireup="false" Codebehind="card_caller.ascx.cs" Inherits="mynamespace.card_caller" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
..
..
<%@ register Tagprefix="club" Tagname="card_display" Src="/somefolder/card_display.ascx"%>
<h1> <%# _firstName %> <%# _lastName%></h1>


<div style="float:right;">
    <club:card_display ID="committee_cardView" runat="server" />
</div>

The main ascx code behind (relevant parts):

private void Page_Load(object sender, EventArgs e) {
    getCards();
}
private void getCards() {
   SqlConnection connection = null;
   SqlDataReader reader = null;
   connection = new SqlConnection(ConfigurationData.databaseConnectionString);
   SqlCommand sqlCommand = new SqlCommand("data.dbo.get_card_data", connection);
   card_display cardpanel;
   cardpanel = new card_display();
   cardpanel.DataBind();

    try {
       sqlCommand.CommandType = CommandType.StoredProcedure;
       connection.Open();
       sqlCommand.Parameters.AddWithValue(...);
       sqlCommand.Parameters.AddWithValue(...);
       sqlCommand.Parameters.AddWithValue(...);
       reader = sqlCommand.ExecuteReader();
       cardpanel.setDataSource(reader);

     } 
    finally {
      if (reader != null) {
        reader.Close();
      }
      if (connection != null) {
        connection.Close();
      }
    }
}

On to the custom control. Here's the relevant part of the user control - the repeater. Nothing too strange going on here. I'm using OnItemDataBound.

<asp:repeater id="card_repeater" EnableViewState="true" OnItemDataBound="card_repeater_ItemDataBound" Visible="True" runat="server">
    <itemtemplate>
      <!-- stuff goes in here -->
    </itemtemplate>
</asp:repeater>

Here's the relevant part of the custom control

namespace mynamespace {
/// <summary>
/// card view display
/// </summary>
[Serializable]
public class card_display : BaseModuleControl {

        private void Page_Load(object sender, EventArgs e) {
        card_repeater.ItemDataBound +=new RepeaterItemEventHandler(card_repeater_ItemDataBound);
        }

        public void setDataSource(SqlDataReader reader) {
            card_repeater.DataSource = reader;
            card_repeater.DataBind();

        }
    }

        protected void directory_repeater_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem)) {

                _currentRecord = e.Item.DataItem as DbDataRecord;
                <!--Do some stuff -->
            }
        }
    }
}

I'm not entirely sure the event handler is needed in the page load. Perhaps it's not binding due to an accessibility issue?

The debugger makes its way through the setDataSource method, and bombs out at the card_repeater.DataBind().

If I declare card_repeater = new Repeater(); in setDataSource there's no longer an Object reference not set to an instance of an object error - but this is obviously incorrect.

Does anyone see anything obvious here as to why the repeater is not binding? Any assistance or pointers are greatly appreciated.

No correct solution

OTHER TIPS

Resolved this. There were several problems.

In card_display, page load

card_repeater.ItemDataBound +=new RepeaterItemEventHandler(card_repeater_ItemDataBound);

is not needed

in getCards

card_display cardpanel;
cardpanel = new card_display();

is not needed, instead in setDataSource the object needs to be called like

committee_cardView.DataSource=reader;
committee_cardView.DataBind();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top