You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复System.InvalidOperationException: ConnectionString属性未初始化错误?

解决ConnectionString属性未初始化的问题

问题描述

即将完成学校任务时遇到最后一个问题,无法修复,相关代码及错误信息如下:

ASPX页面代码

<%@ Page Language="C#" AutoEventWireup="true" 
CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication5.WebForm1" 
%>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title> Progress Reports </title>
</head>
<body>
    <form id="form1" runat="server">
      <div>
  <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
  ProviderName= "System.Data.SqlClient" 
   SelectCommand="SELECT * FROM ProgressReport">
  </asp:SqlDataSource>
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellPadding="4" ForeColor="#333333" DataSourceID="SqlDataSource1">

        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>
  </div>
</form>
</body>
</html>

错误信息

[InvalidOperationException: ConnectionString属性尚未初始化。]
System.Data.SqlClient.SqlConnection.PermissionDemand() +7151089
System.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection outerConnection) +32
System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) +315 System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry) +128
System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +265
System.Data.SqlClient.SqlConnection.Open() +133
System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +182
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +180
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable) +123
System.Web.UI.WebControls.SqlDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +2947
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +26
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +134
System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +106
System.Web.UI.Control.EnsureChildControls() +107
System.Web.UI.Control.PreRenderRecursiveInternal() +58
System.Web.UI.Control.PreRenderRecursiveInternal() +227
System.Web.UI.Control.PreRenderRecursiveInternal() +227
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3671

解决步骤

1. 配置SqlDataSource的ConnectionString

错误核心是SqlDataSource1未配置连接字符串,无法建立数据库连接,有两种配置方式:

方式一:直接在控件中定义连接字符串

修改SqlDataSource1,添加ConnectionString属性:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
  ProviderName="System.Data.SqlClient" 
  ConnectionString="Data Source=你的数据库服务器地址;Initial Catalog=你的数据库名称;Integrated Security=True;"
  SelectCommand="SELECT * FROM ProgressReport">
</asp:SqlDataSource>
  • 替换你的数据库服务器地址(如localhost或远程服务器IP)和你的数据库名称为实际值
  • 若使用SQL Server身份验证,将Integrated Security=True替换为User ID=你的用户名;Password=你的密码;

方式二:引用Web.config中的连接字符串(推荐)

先在Web.config的<configuration>节点下添加连接字符串配置:

<configuration>
  <connectionStrings>
    <add name="ProgressReportConn" 
         connectionString="Data Source=你的数据库服务器地址;Initial Catalog=你的数据库名称;Integrated Security=True;" 
         providerName="System.Data.SqlClient" />
  </connectionStrings>
  <!-- 其他原有配置 -->
</configuration>

再修改SqlDataSource1引用该连接字符串:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
  ProviderName="System.Data.SqlClient" 
  ConnectionString="<%$ ConnectionStrings:ProgressReportConn %>"
  SelectCommand="SELECT * FROM ProgressReport">
</asp:SqlDataSource>

2. 修复GridView列显示问题

你的GridView设置了AutoGenerateColumns="false"但未定义任何列,运行后不会展示数据,可二选一处理:

  • 将AutoGenerateColumns="false"改为AutoGenerateColumns="true",自动生成所有表字段列
  • 添加<Columns>节点手动定义要显示的列,示例:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellPadding="4" ForeColor="#333333" DataSourceID="SqlDataSource1">
  <!-- 原有样式保留 -->
  <Columns>
    <asp:BoundField DataField="ID" HeaderText="报告ID" />
    <asp:BoundField DataField="ReportTitle" HeaderText="报告标题" />
    <asp:BoundField DataField="CreateDate" HeaderText="创建日期" />
    <!-- 根据ProgressReport表的实际字段添加对应BoundField -->
  </Columns>
  <!-- 原有样式保留 -->
</asp:GridView>

内容的提问来源于stack exchange,提问作者WombedHades

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 18:45:38