Search code examples
asp.netcountevalrepeater

DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'userid'


I want to find the total number of active users who are members of the user table in my database. But I get an error like this:

DataBinding: 'System.Data.DataRowView' does not contain a property with name 'userid'

My code is as follows:

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
    <ItemTemplate>
        <h3><%#Eval("userid") %></h3>
    </ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:aytasarimConnectionString %>" SelectCommand="SELECT COUNT(*) FROM [user]"></asp:SqlDataSource>

Solution

  • The error means that there is no column named userid in the result from your query. And that makes sense since you are returning only one column. And you are not even naming that column with AS, so SQL makes it Expr1 as column name.

    your query should be

    SELECT COUNT(*) AS total_users FROM [user]
    

    Now you can use the column name total_users in the Repeater

    <h3><%#Eval("total_users") %></h3>