How do I bind/present several Arraylists to a WebForm?

M

Morten Hauge

Hi!

I have a problem, I'm not sure if this is the proper way to do it, but I
have the following scenario:
I want to list all categories, and all products in that category whenever I
select a topcategory in a menu.

Example:

- Music
- pop < ------pressing this menuitem should give following result:

CDs
------------------------------------------------------
ProductID ProductName ProductDescription
ProductID ProductName ProductDescription
ProductID ProductName ProductDescription

DVDs
------------------------------------------------------
ProductID ProductName ProductDescription
ProductID ProductName ProductDescription
ProductID ProductName ProductDescription

LPs
------------------------------------------------------
ProductID ProductName ProductDescription
ProductID ProductName ProductDescription
ProductID ProductName ProductDescription


I've done this so far, ( se code below )
here I will get the arraylist printed the following way:

For i = 0 To arrProducts.Count - 1
Response.Write(arrProducts(i).ProductID & " -")
Response.Write(arrProducts(i).ProductName & "-")
Response.Write(arrProducts(i).ProductDescription & "<br>")
Next

but as you know Response.Write isn't the way to do it :)
I need some help binding this to a webForm. Would really like to bind this
to a table or anything more structured....
(working in VB and CodeBehind)

'Code behind start**********************************************************

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
LoopCategories()
End Sub

Sub LoopCategories()
Dim CategoryName As String, arrProducts As ArrayList
Dim Conn As New SqlConnection(variables.ConnString)
Dim Cmd As New SqlCommand("SELECT * FROM tblCategory WHERE Parent =
'376' ", Conn)
Cmd.Connection.Open()
Dim dr As SqlDataReader = Cmd.ExecuteReader
While dr.Read

CategoryName = dr.Item("name")
arrProducts = GetProducts(dr.Item("CatID"))

'Here I have put the Categoryname in a varable named: CategoryName
'And an arraylist containing all products in this spesific Category

End While
Cmd.Connection.Close()
Cmd.Dispose()
Conn.Dispose()
End Sub

Function GetProducts(ByVal inputCatID As Integer) As ArrayList
Dim arrProducts As New ArrayList()
Dim ConnProducts As New SqlConnection(variables.ConnString)
Dim CmdProducts As New SqlCommand("SELECT * FROM tblArticle WHERE
CatID = @CatID", ConnProducts)
CmdProducts.Connection.Open()
CmdProducts.Parameters.Add(New SqlParameter("@CatID", inputCatID))
Dim dr As SqlDataReader = CmdProducts.ExecuteReader
While dr.Read
arrProducts.Add(New ProductItem(dr.Item("ArtID"),
dr.Item("title"), dr.Item("ingress")))
End While
CmdProducts.Connection.Close()
CmdProducts.Dispose()
ConnProducts.Dispose()
Return arrProducts
End Function


Friend Class ProductItem

Sub New(ByVal ID As Integer, ByVal Name As String, ByVal Description
As String)
ProductID = ID
ProductName = Name
ProductDescription = Description
End Sub

Dim _ProductID As Integer
Public Property ProductID() As Integer
Get
Return _ProductID
End Get
Set(ByVal Value As Integer)
_ProductID = Value
End Set
End Property

Dim _ProductName As String
Public Property ProductName() As String
Get
Return _ProductName
End Get
Set(ByVal Value As String)
_ProductName = Value
End Set
End Property

Dim _ProductDescription As String
Public Property ProductDescription() As String
Get
Return _ProductDescription
End Get
Set(ByVal Value As String)
_ProductDescription = Value
End Set
End Property
End Class

'code behind ends***************


Thanks for any help!!
Morten......
 
S

Sebastian Wojciechowski

use Repeater, DataList, or DataGrid

Morten Hauge said:
Hi!

I have a problem, I'm not sure if this is the proper way to do it, but I
have the following scenario:
I want to list all categories, and all products in that category whenever
I select a topcategory in a menu.

Example:

- Music
- pop < ------pressing this menuitem should give following result:

CDs
------------------------------------------------------
ProductID ProductName ProductDescription
ProductID ProductName ProductDescription
ProductID ProductName ProductDescription

DVDs
------------------------------------------------------
ProductID ProductName ProductDescription
ProductID ProductName ProductDescription
ProductID ProductName ProductDescription

LPs
------------------------------------------------------
ProductID ProductName ProductDescription
ProductID ProductName ProductDescription
ProductID ProductName ProductDescription


I've done this so far, ( se code below )
here I will get the arraylist printed the following way:

For i = 0 To arrProducts.Count - 1
Response.Write(arrProducts(i).ProductID & " -")
Response.Write(arrProducts(i).ProductName & "-")
Response.Write(arrProducts(i).ProductDescription & "<br>")
Next

but as you know Response.Write isn't the way to do it :)
I need some help binding this to a webForm. Would really like to bind this
to a table or anything more structured....
(working in VB and CodeBehind)

'Code behind
start**********************************************************

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
LoopCategories()
End Sub

Sub LoopCategories()
Dim CategoryName As String, arrProducts As ArrayList
Dim Conn As New SqlConnection(variables.ConnString)
Dim Cmd As New SqlCommand("SELECT * FROM tblCategory WHERE Parent =
'376' ", Conn)
Cmd.Connection.Open()
Dim dr As SqlDataReader = Cmd.ExecuteReader
While dr.Read

CategoryName = dr.Item("name")
arrProducts = GetProducts(dr.Item("CatID"))

'Here I have put the Categoryname in a varable named: CategoryName
'And an arraylist containing all products in this spesific Category

End While
Cmd.Connection.Close()
Cmd.Dispose()
Conn.Dispose()
End Sub

Function GetProducts(ByVal inputCatID As Integer) As ArrayList
Dim arrProducts As New ArrayList()
Dim ConnProducts As New SqlConnection(variables.ConnString)
Dim CmdProducts As New SqlCommand("SELECT * FROM tblArticle WHERE
CatID = @CatID", ConnProducts)
CmdProducts.Connection.Open()
CmdProducts.Parameters.Add(New SqlParameter("@CatID", inputCatID))
Dim dr As SqlDataReader = CmdProducts.ExecuteReader
While dr.Read
arrProducts.Add(New ProductItem(dr.Item("ArtID"),
dr.Item("title"), dr.Item("ingress")))
End While
CmdProducts.Connection.Close()
CmdProducts.Dispose()
ConnProducts.Dispose()
Return arrProducts
End Function


Friend Class ProductItem

Sub New(ByVal ID As Integer, ByVal Name As String, ByVal
Description As String)
ProductID = ID
ProductName = Name
ProductDescription = Description
End Sub

Dim _ProductID As Integer
Public Property ProductID() As Integer
Get
Return _ProductID
End Get
Set(ByVal Value As Integer)
_ProductID = Value
End Set
End Property

Dim _ProductName As String
Public Property ProductName() As String
Get
Return _ProductName
End Get
Set(ByVal Value As String)
_ProductName = Value
End Set
End Property

Dim _ProductDescription As String
Public Property ProductDescription() As String
Get
Return _ProductDescription
End Get
Set(ByVal Value As String)
_ProductDescription = Value
End Set
End Property
End Class

'code behind ends***************


Thanks for any help!!
Morten......
 
M

Morten Hauge

Thanks for a quick answer, yes I have figured out that datagrid,datalistor
repeater is the proper way of doing this,
But I'm not quite sure how I bind a ArrayList to a datagrid...
If theres any other way of doing this, I will be more than glad for some
examples..

Thanks!
Morten
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top