0

I am querying the database to fill the Datagrid. I dont want everything in the database to be displayed on the table just some important information.

I am new to VB.net I am used to asp.net core MVC and entity frameworkork

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

Here is my Datagrid. I just want to display like 5 item from the DB, others in the DB column are not necessary for the user

    <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>
3
  • Why not just query only the fields you required rather than Select * from NotifyMe. Select Id, CustomerName.... From NotifyMe
    – Hursey
    Commented Feb 10, 2021 at 19:52
  • That code should work. Try to clear the solution and rebuild everything. If the error persist use the debugger to check what happens in that code. Side note, your variable names are very confusing.
    – Steve
    Commented Feb 10, 2021 at 19:54
  • then select only part of the data "SELECT * FROM [dbo].[notifyMe]" --> "SELECT *col1, col2, ... FROM [dbo].[notifyMe]"
    – T.S.
    Commented Feb 11, 2021 at 0:54

1 Answer 1

0

"The beginning of wisdom is to call things their proper names"

So your code can be simplified and straightened out a bit:

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.