Click to See Complete Forum and Search --> : How to fill a List(Of )


mutlyp
October 25th, 2011, 03:39 PM
Hope someone can help me with this I just can't get my head around it.
I got this code off the net:
---------------------------------------------------------------------------------------------

Public Class Book
Private m_Name As String
Private m_Chapters As List(Of Chapter)

Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal value As String)
m_Name = value
End Set
End Property

Public Property Chapters() As List(Of Chapter)
Get
Return m_Chapters
End Get
Set(ByVal value As List(Of Chapter))
m_Chapters = value
End Set
End Property

End Class


Public Class Chapter
Private m_Title As String
Private m_Page As String

Public Property Title() As String
Get
Return m_Title
End Get
Set(ByVal value As String)
m_Title = value
End Set
End Property

Public Property Page() As String
Get
Return m_Page
End Get
Set(ByVal value As String)
m_Page = value
End Set
End Property

End Class

Public Sub New()
InitializeComponent()
Dim books As New ObservableCollection(Of Book)()

For i As Integer = 0 To 4
Dim book As New Book() With {.Name = "Book" + i.ToString()}

book.Chapters = New List(Of Chapter)()

For j As Integer = 0 To 4
book.Chapters.Add(New Chapter() With {.Title = "Chapter" + i.ToString() + "_" + j.ToString()})
Next

books.Add(book)
Next

Me.MainList.ItemsSource = books
End Sub

----------------------------------------------------------------------------------------------------
As you can see there is a List(Of Chapter)
And in the code it is populated with Hard Codeing "Chapter" into book.Chapters.Add
I need to know how do you do this with data from a SQL Database table.
So if I had two tables in a database one called Books and the other called Chapters
Books

BookID1 Book1

BookID2 Book2



Chapters

ChapterID1 BookID1 Chapter1

ChapterID2 BookID1 Chapter2

ChapterID3 BookID2 Chapter1a

ChapterID4 BookID2 Chapter2a

ChapterID5 BookID2 Chapter3a

How do I get this data into the List(of )?
Please help
thank you

Cimperiali
February 27th, 2012, 12:06 PM
have a search for
SqlConnection
SqlCommand
SqlDataReader

when you're able to fill a sqlDataReader with the book data, you will be able to cycle thorugh records to load items of a List Of.
And while loading items of the list of books, you will also be able to query the db again to retrieve a sqlDataReader
full of chapters for the book you're on and to load those chapters in the appropriate List Of Capthers property of a Book object....

Cimperiali
February 28th, 2012, 04:33 AM
Here a sample -it is one of the thousand way you could do it. The sample is a windows form done with VS 2010.
Even if it seems you could be working on WPF, the way to retrieve data from db can be the same.
I chosed the simplest.
Look inside SqlHelper.vb class and follow the comments. You will need to adjust connectionString and either to provide correct queries or to insert correct tables and fields in your db


By the way: you used an Observable class. I did not implement the pattern that it seems to be adequate for those kind of objects. If you want to explore it, have a look here
http://msdn.microsoft.com/en-us/library/dd990377.aspx

Cimpy