显示aspnet.db中的数据

时间:2014-10-20 09:48:45

标签: 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 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的用户的页面(该应用程序非常友好地为您创建,以便登录功能正常工作):

<%@ 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>

最后但并非最不重要的是,这是应该为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();
        }
    }
}

我做错了什么?我的意思是msd的人,在他们的教程中可以这样显示数据:(。

提前感谢您试图帮助我。

1 个答案:

答案 0 :(得分:1)

这一行:

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

是问题的根源。根据错误消息,DataSource期望它可以迭代的东西,提取单个值并绑定到网格。或者它可能是实现IDataSource的东西。 GridView无法猜测它应该从您提供的管理器对象中调用哪个方法。

所以这里的数据绑定基本上应该是这样的:

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