C# 显示来自aspnet.db的数据

C# 显示来自aspnet.db的数据,c#,asp.net,C#,Asp.net,在花了相当长的时间尝试配置web.config之后,我终于完成了配置,只是为了得到以下错误: 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 IListSou

在花了相当长的时间尝试配置web.config之后,我终于完成了配置,只是为了得到以下错误:

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.
我正在尝试连接到aspnet.db(我想我这样做是因为我没有连接错误,我从应用程序提供的普通web.config复制粘贴了它) 这是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>

这是我试图显示来自AspNetUsers的用户的页面(应用程序非常友好地为您创建了该页面,以便登录功能正常工作):


|
最后但并非最不重要的一点是,页面背后的代码应该向GridView提供数据:

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();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Web.Security;
使用Owin;
使用SiteStiri.Models;
使用Microsoft.AspNet.Identity;
使用Microsoft.AspNet.Identity.Owin;
命名空间SiteStiri.Administration
{
公共部分类ManageUsers:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!Page.IsPostBack)
{
BindUserAccounts();
}
}
私有void BindUserAccounts()
{
UserAccounts.DataSource=Context.GetOwinContext().GetUserManager();
UserAccounts.DataBind();
}
私有void BindFilteringUI(){
字符串[]过滤器选项={“全部”、“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=过滤器选项;
FilteringUI.DataBind();
}
}
}
我做错了什么?我是说msd的人,在他们的教程中可以这样显示数据:(

提前感谢您试图帮助我。

这句话:

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

你的答案很完美。有一个问题。这个出色的UserManager没有检索所有用户的电子邮件/ID/或任何其他内容的方法,它只能检索1…这让我非常难过…完成了。非常感谢andrei。我设法为用户创建了一个列表:D.var manager=Context.GetOwinContext().GetUserManager();UserAccounts.DataSource=manager.Users.ToList();UserAccounts.DataBind();这是你第二次帮助我了。我非常爱你!@LucianTarna很高兴你帮了我。我通常会在asp.net标签附近闲逛,所以我们可能会再次见面
UserAccounts.DataSource = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
UserAccounts.DataSource = manager.GetUserList(); // Just a made up method, use what you need here
UserAccounts.DataBind();