VB.net 中的数据源类型无效

Data source is an invalid type in VB.net

我正在查询数据库以填充数据网格。我不希望数据库中的所有内容都显示在 table 上,只是一些重要信息。

我是新手 VB.net 我已经习惯 asp.net 核心 MVC 和实体框架

Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource. Here is my code below

    Private Sub GetTSANotification()
        Dim query As String = "SELECT * FROM [dbo].[notifyMe]"

        Dim DBConnection As String = System.Configuration.ConfigurationManager.ConnectionStrings("Notification").ConnectionString
        Using cmd As New SqlConnection(DBConnection)
            Using da As New SqlCommand(query)
                Using sda As New SqlDataAdapter()
                    da.Connection = cmd
                    sda.SelectCommand = da
                    Using data As New DataTable()
                        sda.Fill(data)
                        notifyReport.DataSource = da
                        notifyReport.DataBind()
                    End Using
                End Using
                End Using
            End Using
    End Sub

这是我的数据网格。 我只想显示数据库中的 5 个项目,用户不需要数据库列中的其他项目

    <asp:GridView CssClass="table table-bordered table-striped" ID="notifyReport" runat="server" AutoGenerateColumns="false" AllowPaging="true" OnPageIndexChanging="OnPageIndexChanging" PageSize="10">
                                    
                                    <Columns>
                                        <asp:BoundField DataField="id" HeaderText="Id" SortExpression="id"></asp:BoundField>
                                        <asp:BoundField DataField="customerName" HeaderText="Customer Name" SortExpression="customerName"></asp:BoundField>
                                        <asp:BoundField DataField="customerEmail" HeaderText="Email" SortExpression="customerEmail"></asp:BoundField>
                                        <asp:BoundField DataField="fee" HeaderText="Fee" SortExpression="fee"></asp:BoundField>
                                        <asp:BoundField DataField="feedType" HeaderText="Feed Type" ReadOnly="True" SortExpression="feedType"></asp:BoundField>
                                         <asp:BoundField DataField="narrationDesc" HeaderText="Narration" ReadOnly="True" SortExpression="narrationDesc"></asp:BoundField>
                                    </Columns>
                                </asp:GridView>

“智慧的开端是给事物起正确的名字”

所以你的代码可以稍微简化和理顺一下:

Private Sub GetTSANotification()
    Dim query As String = "SELECT * FROM [dbo].[notifyMe]"

    Dim connStr As String = System.Configuration.ConfigurationManager.ConnectionStrings("Notification").ConnectionString

   
    Using sda As New SqlDataAdapter(query, connStr)
      Dim data As New DataTable
      sda.Fill(data)
      notifyReport.DataSource = data
      notifyReport.DataBind()
    End Using
            
End Sub