Lucian Tarna
Lucian Tarna

Reputation: 1827

Displaying data from aspnet.db

After quite some time spent trying to configure web.config i finally done it just to get the following error :

An exception of type 'System.InvalidOperationException' occurred in System.Web.dll but was not handled in user code

Additional information: Data source is an invalid type.  It must be either an IListSource, IEnumerable, or IDataSource.

I am trying to connect to aspnet.db (i think i did it because i have no connection error , i copy pasted it from the normal web.config that the aplication provides you with) This is the web.config:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-SiteStiri-20141013120414.mdf;Initial Catalog=aspnet-SiteStiri-20141013120414;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  <add name="StiriDB"
      connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\SiteStiri.mdf;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <pages>
      <namespaces>
        <add namespace="System.Web.Optimization" />
        <add namespace="Microsoft.AspNet.Identity" />
      </namespaces>
      <controls>
        <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />
      </controls>
    </pages>
    <membership>
      <providers>
        <!--
          ASP.NET Membership is disabled in this template. Please visit the following link http://go.microsoft.com/fwlink/?LinkId=301889 to learn about the ASP.NET Membership support in this template
        -->
        <clear />
      </providers>
    </membership>
    <authorization> 
       <allow roles="canEdit" />
        <deny users="*"/>
     </authorization> 
    <profile>
      <providers>
        <!--
          ASP.NET Membership Profile is disabled in this template. Please visit the following link http://go.microsoft.com/fwlink/?LinkId=301889 to learn about the ASP.NET Membership support in this template
        -->
        <clear />
      </providers>
    </profile>
    <roleManager>
      <!--
            ASP.NET Membership Role is disabled in this template. Please visit the following link http://go.microsoft.com/fwlink/?LinkId=301889 to learn about the ASP.NET Membership support in this template
        -->
      <providers>
        <clear />
      </providers>
    </roleManager>
    <!--
            If you are deploying to a cloud environment that has multiple web server instances,
            you should change session state mode from "InProc" to "Custom". In addition,
            change the connection string named "DefaultConnection" to connect to an instance
            of SQL Server (including SQL Azure and SQL  Compact) instead of to SQL Server Express.
      -->
    <sessionState mode="InProc" customProvider="DefaultSessionProvider">
      <providers>
        <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
      </providers>
    </sessionState>
  </system.web>
  <system.webServer>
    <modules>
      <remove name="FormsAuthenticationModule" />
    </modules>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" culture="neutral" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

This is the page where i am trying to display the users from AspNetUsers(which the aplication very kindly creates for you so that the log in functionality works):

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="ManageUsers.aspx.cs" Inherits="SiteStiri.Administration.ManageUsers" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">

    <asp:Repeater ID="FilteringUI" runat="server">
         <ItemTemplate> 
             <asp:LinkButton runat="server" ID="lnkFilter" Text='<%# Container.DataItem %>' CommandName='<%# Container.DataItem %>'>

             </asp:LinkButton> 

         </ItemTemplate> 
        <SeparatorTemplate>|</SeparatorTemplate>

    </asp:Repeater>

    <asp:GridView ID="UserAccounts" runat="server" AutoGenerateColumns="False">
         <Columns> <asp:BoundField DataField="UserName" HeaderText="UserName"/> 
             <asp:BoundField DataField="Email" HeaderText="Email" /> 
             <asp:CheckBoxField DataField="IsApproved" HeaderText="Approved?"/> 
             <asp:CheckBoxField DataField="IsLockedOut" HeaderText="Locked Out?" /> 
             <asp:CheckBoxField DataField="IsOnline" HeaderText="Online?"/> 
             <asp:BoundField DataField="Comment" HeaderText="Comment"/> 

         </Columns> 

    </asp:GridView>


</asp:Content>

Last but not least this is the code behind the page that should feed the GridView with data :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using Owin;
using SiteStiri.Models;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;

namespace SiteStiri.Administration
{
    public partial class ManageUsers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindUserAccounts();

            }
        }
        private void BindUserAccounts()
        {
            UserAccounts.DataSource = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
            UserAccounts.DataBind();
        }

        private void BindFilteringUI() { 
            string[] filterOptions = { "All", "A", "B", "C", "D", "E", "F", "G", "H",
                                         "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
            FilteringUI.DataSource = filterOptions; 
            FilteringUI.DataBind();
        }
    }
}

What am i doing wrong ? I mean the guys at msd , in their tutorial can display the data this way :( .

Thank you in advance for trying to help me.

Upvotes: 0

Views: 264

Answers (1)

Andrei
Andrei

Reputation: 56688

This line:

UserAccounts.DataSource = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();

is the source of the problem. As per error message, DataSource expects something it can iterate over, extract individual values and bind the to the grid. Or it could be something that implements IDataSource. GridView cannot guess which method should it call from the manager object you are giving it.

So essentially data binding here should look like this:

var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
UserAccounts.DataSource = manager.GetUserList(); // Just a made up method, use what you need here
UserAccounts.DataBind();

Upvotes: 1

Related Questions