By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,341 Members | 1,409 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,341 IT Pros & Developers. It's quick & easy.

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

P: n/a
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......
Nov 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

use Repeater, DataList, or DataGrid

"Morten Hauge" <mo****@april.no> wrote in message
news:e1**************@TK2MSFTNGP10.phx.gbl...
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......

Nov 19 '05 #2

P: n/a
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

"Sebastian Wojciechowski" <so*****@microsoft.com> skrev i melding
news:uZ**************@TK2MSFTNGP12.phx.gbl...

use Repeater, DataList, or DataGrid

"Morten Hauge" <mo****@april.no> wrote in message
news:e1**************@TK2MSFTNGP10.phx.gbl...
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......


Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.