{"id":587,"date":"2004-04-12T09:51:37","date_gmt":"2004-04-12T13:51:37","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=587"},"modified":"2004-04-12T09:51:37","modified_gmt":"2004-04-12T13:51:37","slug":"aspnet-data-binding-array-of-hashtables","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2004\/04\/12\/aspnet-data-binding-array-of-hashtables\/","title":{"rendered":"ASP.NET: Data-binding array of hashtables"},"content":{"rendered":"<p>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 &lt;asp:repeater&gt;.  Looks like some other ASP.NETers had the <a href=\"http:\/\/www.dotnet247.com\/247reference\/msgs\/17\/87023.aspx\">same problem<\/a>, but the &#8216;solutions&#8217; they offered didn&#8217;t seem to solve the errors I was getting.  First, in the code behind, I had something like this in the code behind:<br \/>\n<code><br \/>\npublic Repeater events = new Repeater();<br \/>\nprivate ArrayList aEvents = new ArrayList();<br \/>\npublic void Page_Load(Object sender, EventArgs e) {<br \/>\n&nbsp;&nbsp;Hashtable event1 = new Hashtable();<br \/>\n&nbsp;&nbsp;event1.Add(\"name\", \"name of event 1\");<br \/>\n&nbsp;&nbsp;event1.Add(\"date\", \"5\/31\/2004\");<br \/>\n&nbsp;&nbsp;Hashtable event2 = new Hashtable();<br \/>\n&nbsp;&nbsp;event2.Add(\"name\", \"name of event 2\");<br \/>\n&nbsp;&nbsp;event2.Add(\"date\", \"06\/20\/2004\");<br \/>\n&nbsp;&nbsp;aEvents.Add(event1);<br \/>\n&nbsp;&nbsp;aEvents.Add(event2);<br \/>\n&nbsp;&nbsp;events.DataSource = aEvents;<br \/>\n&nbsp;&nbsp;events.DataBind();<br \/>\n}<br \/>\n<\/code><br \/>\nand then a standard repeater tag in my ASP.NET code:<br \/>\n<code><br \/>\n&lt;asp:repeater id=\"events\" runat=\"server\"&gt;<br \/>\n&nbsp;&nbsp;&lt;ItemTemplate&gt;<br \/>\n&nbsp;&nbsp;Name:&lt;%# DataBinder.Eval(Container.DataItem,\"name\") %&gt;<br \/>\n&nbsp;&nbsp;Date:&lt;%# DataBinder.Eval(Container.DataItem,\"date\") %&gt;<br \/>\n&nbsp;&nbsp;&lt;\/ItemTemplate&gt;<br \/>\n&lt;\/asp:repeater&gt;<br \/>\n<\/code><br \/>\nThe first error I got was <code>DataBinder.Eval: 'System.Collections.Hashtable' does not contain a property with the name name.<\/code>, which means that the DataBinder uses reflection to find a property called &#8216;name&#8217; on the current object in the iteration. This is a problem because obviously a hashtable doesn&#8217;t have a public property for &#8216;name&#8217;, it only has an associate array of values. The workaround, although I don&#8217;t know why this works, is to wrap the keys in a brackets:<br \/>\n<code><br \/>\nName:&lt;%# DataBinder.Eval(Container.DataItem,\"[name]\") %&gt;<br \/>\nDate:&lt;%# DataBinder.Eval(Container.DataItem,\"[date]\") %&gt;<br \/>\n<\/code><br \/>\nUsing brackets lets you use an array of hashtables as a datasource. Since this works, it might be helpful to add it to the <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpgenref\/html\/cpcondatabindingexpressionsyntax.asp\">Data Binding Expression Syntax<\/a> guide on MSDN.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &lt;asp:repeater&gt;. Looks like some other ASP.NETers had the same problem, but the &#8216;solutions&#8217; they offered didn&#8217;t seem to solve the errors I was getting. First, in the code behind, I had &hellip; <a href=\"https:\/\/cephas.net\/blog\/2004\/04\/12\/aspnet-data-binding-array-of-hashtables\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">ASP.NET: Data-binding array of hashtables<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[17],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/587"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=587"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/587\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}