Count aggregate
27 February 2008 Tips  Marek Sienkiewicz
Bubble image
I wanted to show the number of listings in a particular category. After some research and reading Scott Mitchell's book ASP.NET Data Web Controls, I came up with the following solution.

Code behind page under the Page_Load event

Dim cnn As New SqlConnection
cnn.ConnectionString = _
"Data Source = yourDataSource;" & _
"Initial Catalog = yourDatabase;" & _
"Integrated Security = SSPI"

Dim da As SqlDataAdapter
da = New SqlDataadapter(

SELECT category, COUNT(*)
FROM myTable
GROUP BY Category ", cnn
)

Dim ds As New DataSet
da.Fill(ds,"Category")

dlCategory.DataSource = ds
DataBind()

Then on the html page

<asp:DataList ID="dlCategory" runat="server">

<itemTemplate>
<a href ='Categories.aspx?cat=<%#  Eval("catergory")  %>'>
<%# Eval("category") %>
<%# Eval("categoryCount") %></a>
</itemTemplate>

</asp:DataList>

And so now if you look under the categories column on the right a count is displayed against each of my categories. All that remains for me to do now is an Archive and the Search





Please post your comments:

Name

Email
                           

   

Enter the text you see in the box:

Captcha Image