显示aspnet.db中的数据

本文关键字:数据 db aspnet 显示 | 更新日期: 2023-09-27 18:27:12

在花了很长时间尝试配置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的家伙,在他们的教程中可以这样显示数据:(.

提前感谢您的帮助。

显示aspnet.db中的数据

此行:

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();