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

How to bind DropdDownList to ArrayList

P: n/a
Hello,

I have a code that queries Active Directory and put logins and names into
the ArrayList. Then I need to bind this ArrayList to the DropdDownList,
making names as DataTextField and logins as DataValueFields.

I have no problem doing it, if I have only logins or only names, since
ArrayList always has one dimension. But I need them both. So the code looks
like this:

Sub Getallusers(ByVal selddl As DropDownList)
Try
Dim sAdsPath As String = "LDAP://seart01/dc=acdtraining,dc=com"
Dim de As New DirectoryEntry(sAdsPath, "Domain\user",
"password")
Dim ds1 As New DirectorySearcher(de)
ds1.Filter = "(objectCategory=person)"
ds1.Filter = "(objectClass=user)"
ds1.Filter = "(employeeType=ALT)"
'Dim colServers(100, 100) As String
Dim colServers As ArrayList
colServers = New ArrayList
Dim result As SearchResult

For Each result In ds1.FindAll()
colServers.Add(New String()
{result.Properties("displayName")(0),
result.Properties("samAccountName")(0)})
Next result
'colServers.Sort()
selddl.DataSource = colServers

??????????????????????????????????

selddl.DataBind()
de.Dispose()
Catch ex As Exception
Dim s As String = ex.Message
End Try
End Sub

The ArrayList is filling up OK, now each value contains two values - Name
(0) and Login(1). But how to extract them separately and make one a text
field and another - value field? Tried everything, but couldn't fugure out.
It's definitely possible, since each value has it's own index (0 or 1), but
I don't know how.

Alternatively, how would I use just an Array instead?

I would appreciate your help very much.

Thank you,

--
Peter
Nov 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Here's a reference example that uses a similar technique:

Sub PopulateDropDownList(ByVal ddl As DropDownList)
Dim al As New ArrayList()
For i As Integer = 0 To 10
al.Add(New LoginClass("Login: " & i.ToString(), "Name: " &
i.ToString()))
Next
ddl.DataSource = al
ddl.DataTextField = "Login"
ddl.DataValueField = "Name"
ddl.DataBind()
End Sub

Class LoginClass
Public Readonly Property Name() As String
Get
Return _name
End Get
End Property

Public Readonly Property Login() As String
Get
Return _login
End Get
End Property
Private _login As String
Private _name As String
Public Sub New(ByVal login As String, ByVal name As String)
_login = login
_name = name
End Sub
End Class

Hope I've helped.

-Alan

Nov 21 '05 #2

P: n/a
Thank you very much, Alan. I've heard that creating a class would be a best
solution, but wasn't sure how to do this.

Peter

"Alan Samet" <al*******@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Here's a reference example that uses a similar technique:

Sub PopulateDropDownList(ByVal ddl As DropDownList)
Dim al As New ArrayList()
For i As Integer = 0 To 10
al.Add(New LoginClass("Login: " & i.ToString(), "Name: " &
i.ToString()))
Next
ddl.DataSource = al
ddl.DataTextField = "Login"
ddl.DataValueField = "Name"
ddl.DataBind()
End Sub

Class LoginClass
Public Readonly Property Name() As String
Get
Return _name
End Get
End Property

Public Readonly Property Login() As String
Get
Return _login
End Get
End Property
Private _login As String
Private _name As String
Public Sub New(ByVal login As String, ByVal name As String)
_login = login
_name = name
End Sub
End Class

Hope I've helped.

-Alan

Nov 21 '05 #3

P: n/a
Peter,

You add an array of two strings to your arraylist, therefore probably what
you want is to get the first two arraylist items.

\\\
dim mystring1 as string = directcast(arraylist(0),string())(0)
and
dim mystring2 as string = directcast(arraylist(0),string())(1)
///
Roughly typed not tested.

I hope this helps,

Cor
Nov 21 '05 #4

P: n/a
Peter,

A much easier solution is building a datatable and use that (has as well not
so much overhead by extra classes, if needed sorting etc because all code is
build in standard in the system.net namespace for that).

\\\
dim dt as datatable
dt.columns.add("person")
dt.columns.add("user")
for each in................
dt.rows.add(dt.newrow)
dt.rows(0) = first item
dt.rows(1) = second itme
next
drpdown.datasource = dt
drpdown.datatextfield = "person"
drpdown.datavalufield = "user"
drpdown.databind
////

I hope this helps,

Cor

"Peter Afonin" <pv*@speakeasy.net> schreef in bericht
news:eC****************@TK2MSFTNGP11.phx.gbl...
Hello,

I have a code that queries Active Directory and put logins and names into
the ArrayList. Then I need to bind this ArrayList to the DropdDownList,
making names as DataTextField and logins as DataValueFields.

I have no problem doing it, if I have only logins or only names, since
ArrayList always has one dimension. But I need them both. So the code
looks
like this:

Sub Getallusers(ByVal selddl As DropDownList)
Try
Dim sAdsPath As String = "LDAP://seart01/dc=acdtraining,dc=com"
Dim de As New DirectoryEntry(sAdsPath, "Domain\user",
"password")
Dim ds1 As New DirectorySearcher(de)
ds1.Filter = "(objectCategory=person)"
ds1.Filter = "(objectClass=user)"
ds1.Filter = "(employeeType=ALT)"
'Dim colServers(100, 100) As String
Dim colServers As ArrayList
colServers = New ArrayList
Dim result As SearchResult

For Each result In ds1.FindAll()
colServers.Add(New String()
{result.Properties("displayName")(0),
result.Properties("samAccountName")(0)})
Next result
'colServers.Sort()
selddl.DataSource = colServers

??????????????????????????????????

selddl.DataBind()
de.Dispose()
Catch ex As Exception
Dim s As String = ex.Message
End Try
End Sub

The ArrayList is filling up OK, now each value contains two values - Name
(0) and Login(1). But how to extract them separately and make one a text
field and another - value field? Tried everything, but couldn't fugure
out.
It's definitely possible, since each value has it's own index (0 or 1),
but
I don't know how.

Alternatively, how would I use just an Array instead?

I would appreciate your help very much.

Thank you,

--
Peter

Nov 21 '05 #5

P: n/a
Ha -- funny I didn't think of it earlier. You may also use the
sortedlist or hashtable classes to accomplish key-value databinding as
well.

example:

Sub PopulateDropDownList(ByVal ddl As DropDownList)
Dim sl As New SortedList() 'Hashtable()
For i As Integer = 0 To 10
sl.Add("Login: " & i.ToString(), "Name: " & i.ToString())
Next
ddl.DataSource = sl
ddl.DataTextField = "Key"
ddl.DataValueField = "Value"
ddl.DataBind()
End Sub

Nov 21 '05 #6

P: n/a
Thank you very much, Cor. I'll try it your way. I've tried before something
similar, but messed something up.

Peter

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:es*************@TK2MSFTNGP12.phx.gbl...
Peter,

A much easier solution is building a datatable and use that (has as well not so much overhead by extra classes, if needed sorting etc because all code is build in standard in the system.net namespace for that).

\\\
dim dt as datatable
dt.columns.add("person")
dt.columns.add("user")
for each in................
dt.rows.add(dt.newrow)
dt.rows(0) = first item
dt.rows(1) = second itme
next
drpdown.datasource = dt
drpdown.datatextfield = "person"
drpdown.datavalufield = "user"
drpdown.databind
////

I hope this helps,

Cor

"Peter Afonin" <pv*@speakeasy.net> schreef in bericht
news:eC****************@TK2MSFTNGP11.phx.gbl...
Hello,

I have a code that queries Active Directory and put logins and names into the ArrayList. Then I need to bind this ArrayList to the DropdDownList,
making names as DataTextField and logins as DataValueFields.

I have no problem doing it, if I have only logins or only names, since
ArrayList always has one dimension. But I need them both. So the code
looks
like this:

Sub Getallusers(ByVal selddl As DropDownList)
Try
Dim sAdsPath As String = "LDAP://seart01/dc=acdtraining,dc=com" Dim de As New DirectoryEntry(sAdsPath, "Domain\user",
"password")
Dim ds1 As New DirectorySearcher(de)
ds1.Filter = "(objectCategory=person)"
ds1.Filter = "(objectClass=user)"
ds1.Filter = "(employeeType=ALT)"
'Dim colServers(100, 100) As String
Dim colServers As ArrayList
colServers = New ArrayList
Dim result As SearchResult

For Each result In ds1.FindAll()
colServers.Add(New String()
{result.Properties("displayName")(0),
result.Properties("samAccountName")(0)})
Next result
'colServers.Sort()
selddl.DataSource = colServers

??????????????????????????????????

selddl.DataBind()
de.Dispose()
Catch ex As Exception
Dim s As String = ex.Message
End Try
End Sub

The ArrayList is filling up OK, now each value contains two values - Name (0) and Login(1). But how to extract them separately and make one a text
field and another - value field? Tried everything, but couldn't fugure
out.
It's definitely possible, since each value has it's own index (0 or 1),
but
I don't know how.

Alternatively, how would I use just an Array instead?

I would appreciate your help very much.

Thank you,

--
Peter


Nov 21 '05 #7

P: n/a
Cor, with a few modifications your solution worked great for me, thank you
very much.

Dim result As SearchResult
Dim dt As DataTable = New DataTable
dt.Columns.Add("Name")
dt.Columns.Add("Login")
Dim n As Integer = 0

For Each result In ds1.FindAll()
dt.Rows.Add(dt.NewRow)
dt.Rows(n)("Name") = result.Properties("displayName")(0)
dt.Rows(n)("Login") = result.Properties("samAccountName")(0)
n = n + 1
Next result

Dim dv As DataView = dt.DefaultView
dv.Sort = "Name"

selddl.DataSource = dv
selddl.DataTextField = "Name"
selddl.DataValueField = "Login"
selddl.DataBind()

Peter

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:es*************@TK2MSFTNGP12.phx.gbl...
Peter,

A much easier solution is building a datatable and use that (has as well not so much overhead by extra classes, if needed sorting etc because all code is build in standard in the system.net namespace for that).

\\\
dim dt as datatable
dt.columns.add("person")
dt.columns.add("user")
for each in................
dt.rows.add(dt.newrow)
dt.rows(0) = first item
dt.rows(1) = second itme
next
drpdown.datasource = dt
drpdown.datatextfield = "person"
drpdown.datavalufield = "user"
drpdown.databind
////

I hope this helps,

Cor

"Peter Afonin" <pv*@speakeasy.net> schreef in bericht
news:eC****************@TK2MSFTNGP11.phx.gbl...
Hello,

I have a code that queries Active Directory and put logins and names into the ArrayList. Then I need to bind this ArrayList to the DropdDownList,
making names as DataTextField and logins as DataValueFields.

I have no problem doing it, if I have only logins or only names, since
ArrayList always has one dimension. But I need them both. So the code
looks
like this:

Sub Getallusers(ByVal selddl As DropDownList)
Try
Dim sAdsPath As String = "LDAP://seart01/dc=acdtraining,dc=com" Dim de As New DirectoryEntry(sAdsPath, "Domain\user",
"password")
Dim ds1 As New DirectorySearcher(de)
ds1.Filter = "(objectCategory=person)"
ds1.Filter = "(objectClass=user)"
ds1.Filter = "(employeeType=ALT)"
'Dim colServers(100, 100) As String
Dim colServers As ArrayList
colServers = New ArrayList
Dim result As SearchResult

For Each result In ds1.FindAll()
colServers.Add(New String()
{result.Properties("displayName")(0),
result.Properties("samAccountName")(0)})
Next result
'colServers.Sort()
selddl.DataSource = colServers

??????????????????????????????????

selddl.DataBind()
de.Dispose()
Catch ex As Exception
Dim s As String = ex.Message
End Try
End Sub

The ArrayList is filling up OK, now each value contains two values - Name (0) and Login(1). But how to extract them separately and make one a text
field and another - value field? Tried everything, but couldn't fugure
out.
It's definitely possible, since each value has it's own index (0 or 1),
but
I don't know how.

Alternatively, how would I use just an Array instead?

I would appreciate your help very much.

Thank you,

--
Peter


Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.