ASP.NET: Data-binding array of hashtables

Spent this morning in ASP.NET land working on a small modification in which I needed to data bind an array of hashtables to an <asp:repeater>. Looks like some other ASP.NETers had the same problem, but the ‘solutions’ they offered didn’t seem to solve the errors I was getting. First, in the code behind, I had something like this in the code behind:

public Repeater events = new Repeater();
private ArrayList aEvents = new ArrayList();
public void Page_Load(Object sender, EventArgs e) {
  Hashtable event1 = new Hashtable();
  event1.Add("name", "name of event 1");
  event1.Add("date", "5/31/2004");
  Hashtable event2 = new Hashtable();
  event2.Add("name", "name of event 2");
  event2.Add("date", "06/20/2004");
  events.DataSource = aEvents;

and then a standard repeater tag in my ASP.NET code:

<asp:repeater id="events" runat="server">
  Name:<%# DataBinder.Eval(Container.DataItem,"name") %>
  Date:<%# DataBinder.Eval(Container.DataItem,"date") %>

The first error I got was DataBinder.Eval: 'System.Collections.Hashtable' does not contain a property with the name name., which means that the DataBinder uses reflection to find a property called ‘name’ on the current object in the iteration. This is a problem because obviously a hashtable doesn’t have a public property for ‘name’, it only has an associate array of values. The workaround, although I don’t know why this works, is to wrap the keys in a brackets:

Name:<%# DataBinder.Eval(Container.DataItem,"[name]") %>
Date:<%# DataBinder.Eval(Container.DataItem,"[date]") %>

Using brackets lets you use an array of hashtables as a datasource. Since this works, it might be helpful to add it to the Data Binding Expression Syntax guide on MSDN.

7 thoughts on “ASP.NET: Data-binding array of hashtables”

  1. Interesting finding Aaron. We’ve had similar things come up and now use the DataTable quite frequently, even for more lightweight pieces of data.
    The databinding piece didn’t manifest itself quite in the way you are describing, it was more that the Hashtable did not store items in the same order that they were put in.
    Now that I’ve used the datatable (and even have some wrappers for it) I’ve gotten used to having the ability to expand to more columns / sort on the fly and so on.
    But it would be interesting to write a class with its own indexer to create a Hash structure that was more databinding friendly.

  2. “‘System.Collections.Hashtable’ does not contain a property with the name name”

    Well this is beacuse the hash table doesn’t have name as a property, but as a key. You need to use the indexer to access the value associated with that key. Here you have used the c# syntax (as I understand it). You can get the same results by using


    It seems to me that using a hash table in this way is great from writing something quick and dirty, but what are the performance implications.? Also, I would look out for errors resulting from mistyped key names in your code.

Leave a Reply

Your email address will not be published. Required fields are marked *