473,382 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How to bind DropdDownList to ArrayList

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 19 '05 #1
7 1383
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 19 '05 #2
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 19 '05 #3
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 19 '05 #4
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 19 '05 #5
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 19 '05 #6
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 19 '05 #7
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 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Morten Hauge | last post by:
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...
3
by: Bob Lehmann | last post by:
Hi, I'm trying to bind an ArrayList to a DataList. I get the error DataBinder.Eval: 'System.String' does not contain a property with the name Name. I don't know what property of ArrayList...
7
by: Peter Afonin | last post by:
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...
1
by: john wright | last post by:
I have a dictionary oject I created and I want to bind a listbox to it. I am including the code for the dictionary object. Here is the error I am getting: "System.Exception: Complex...
3
by: serge calderara | last post by:
Dear all, Does anyone know how to bind a System.Collection.ArraysList object to a Dataset ? Thanks for your reply Regards Serge
3
by: sck10 | last post by:
Hello, I am trying to bind an arraylist to a FormView DropDownList control in the PreRender state. The error that I get is the following: Databinding methods such as Eval(), XPath(), and...
3
by: vineetbatta | last post by:
I have Custom Data class which stores data about single customer and then i store that customer objects in arraylist as shown below. Customer custdata = null; // Custom Data class for 1 customer...
3
by: weird0 | last post by:
Purpose: The objective is to update or add a new row in datagridview using an arraylist I have an arraylist inside of a class and i added an object to the arraylist on the button click event....
0
by: pbd22 | last post by:
Hi. I am returning to an old bit of code in our program and need to figure out how to sort my columns on bind. I am sorting on Date (mostly) and some other values. Problem is, the code is an...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.