c# - 通过gridview显示数据

标签 c# asp.net database gridview connection-string

我正在使用 GridView 从本地数据库读取数据,但出现未处理的异常。

页面显示这些消息:

Illegal characters in path.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Illegal characters in path.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

下面是web.config中的连接字符串

<connectionStrings>
    <add name="automobileDB"
     connectionString="Data Source=(LocalDB)\v11.0;
        AttachDbFilename=|DataDirectory|\     
        automobileDB.mdf; Integrated Security=True" 



    providerName="System.Data.SqlClient"/>
  </connectionStrings>

下面是我在 webform 中的 GridView 代码:

<asp:SqlDataSource ID="ADDataSource" runat="server" 
            ConnectionString="<%$ ConnectionStrings:automobileDB %>"
            SelectCommand="SELECT * FROM [Advertisements]" > 
</asp:SqlDataSource>


<div>
    <asp:GridView ID="AddGridView" runat="server" AllowPaging="True" 
           AutoGenerateColumns="False" CssClass="list" DataKeyNames="Id" 
           DataSourceID="ADDataSource" GridLines="None">
           <Columns>
                 <asp:BoundField DataField="topic" HeaderText="First Name" />
                 <asp:BoundField DataField="Brand" HeaderText="Last Name" />
                 <asp:BoundField DataField="Model" HeaderText="Phone" />
                 <asp:BoundField DataField="Year" HeaderText="Email" />
           </Columns>
       </asp:GridView>

</div>

这里有什么异常(exception)?

谢谢

最佳答案

根据您在 web.config 中编写连接字符串的方式,|DataDirectory|\automobileDB.mdf 之间有一些空间。将您的连接字符串更改为此:

<connectionStrings>
    <add name="automobileDB"
     connectionString="Data Source=(LocalDB)\v11.0;
        AttachDbFilename=|DataDirectory|\automobileDB.mdf; Integrated Security=True" 
    providerName="System.Data.SqlClient"/>
</connectionStrings>

关于c# - 通过gridview显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25963046/

相关文章:

c# - 为什么我不能将 List<T> 传递给通用构造函数参数,尽管所有约束都已满足?

c# - 从数据库获取多个值 ASP.NET/C#

mysql - 语句 'cold state' 是什么意思?

php - 无法将 php session 变量设置到另一个 php 页面

c# - 对于 AES 256 位,在 Java 中加密并在 C# 中解密

c# - 编码以改进 IIS 应用程序池内存使用

c# - Mongodb - 从集合中检索 500 万条记录的最快方法

asp.net - 由于asp.net中对象的当前状态,因此操作无效。

asp.net - Breeze 查询错误,即使结果返回

database - zf3 中的 doctrine2 多数据库连接?