Hope someone can help me with this I just can't get my head around it.
I got this code off the net:
---------------------------------------------------------------------------------------------
Code:
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
Last edited by Cimperiali; February 27th, 2012 at 12:33 PM.
Reason: added [code][/code] tags
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....
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
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
Last edited by Cimperiali; March 5th, 2012 at 04:28 PM.
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.