dcsimg
Results 1 to 3 of 3
  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    225

    Resolved [RESOLVED] Converting datatable to a student object

    I have been trying to convert this datatable into a student object but it has been displaying "Expression of type 'System.Data.Datatable', which is not a collection type.

    Code:
    Dim StudentList As New List(of Student)
    For each Data As DataRow In Table
    For a As Integer = 0 to Data.Columns.Count-1
    
         StudentList.Add(New Student(Data.ID, Data.Name, Data. Subject, Data.Age))
    Next
    Next
    Last edited by TATARPRO; May 17th, 2018 at 10:02 AM.
    Nothing I post is Self Reliable. Use it at your own risk

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,682

    Re: Converting datatable to a student object

    The DataRows are in the Rows collection of the DataTable, not the DataTable itself. Alternatively, you can call AsEnumerable on the DataTable, which returns an IEnumerable(Of DataRow).
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    3,619

    Re: Converting datatable to a student object

    Your don't seem to be accessing the DataTable data correctly, try something like

    Code:
    Dim StudentList As New List(of Student)
    For each Data As DataRow In Table.Rows
    
    
         StudentList.Add(New Student(CInt(Data("ID")), Data("Name").ToString, Data(" Subject").ToString, CInt(Data("Age")))
    
    Next
    This was written free hand and untested, it also assumes that ID and Age are Integers.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.