473,399 Members | 2,478 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,399 software developers and data experts.

index was out of range


Hi;
I am quering Active Directory and putting the results of users in the
listbox. Then the user can click on the listbox which should populate
the
textboxes with the fields from AD, which works fine as long as each
field has
a value but i get Index was out of range when one of the fields is
blank. I
am posting sample of the code here:

Dim lbBox As New ListBox
Dim txtName, txtMail, txtEmployeeID, txtEmployeeNo As New
TextBox
Dim objParent As New DirectorySearcher(Me.txtDomain.Text)

'Me.txtName.Text = " "
Dim cmdEdit As New Button
If Me.lbBox.SelectedItem = "" Then
MsgBox("Make a selection")
End If
objParent.Filter = "(&(objectCategory=user)(Name=" +
Me.lbBox.SelectedItem + "*))"
objParent.PropertiesToLoad.Add("cn")
objParent.PropertiesToLoad.Add("employeeID")
objParent.PropertiesToLoad.Add("employeeNumber")
objParent.PropertiesToLoad.Add("mail")
Dim colQueryResults As SearchResultCollection
colQueryResults = objParent.FindAll()

Dim objResult As SearchResult
For Each objResult In colQueryResults

'Me.lbBox.Items.Add(objResult.Properties("cn")(0))
'Me.lbBox.Items.Add(objResult.Properties("employee ID")(0))
Me.txtName.Text = (objResult.Properties("cn")(0))
Me.txtMail.Text = (objResult.Properties("mail")(0))
Me.txtEmployeeID.Text =
(objResult.Properties("employeeID")(0))
Me.txtEmployeeNo.Text =
(objResult.Properties("employeeNumber")(0))
Next

*** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '05 #1
1 3395
Actually here is rest of the code:

Imports System
Imports System.DirectoryServices
'Imports System.DirectoryServices.DirectorySearcher
Public Class Form1
Private MySearch As String

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdDisabled.Click
Dim objSearch As New DirectorySearcher()
Dim lbBox As New ListBox
Dim lblListbox As New Label
Dim txtDomain As New TextBox

Me.lblListbox.Text = "Dislabled Users: "
Me.lbBox.Items.Clear()
objSearch.SearchRoot = New DirectoryEntry(Me.txtDomain.Text)
objSearch.Filter =
"(&(objectCategory=person)(objectClass=user)(userA ccountControl:1.2.840.113556.1.4.803:=2))"
objSearch.SearchScope = SearchScope.Subtree
objSearch.PropertiesToLoad.Add("cn")

Dim colQueryResults As SearchResultCollection
colQueryResults = objSearch.FindAll()

Dim objResult As SearchResult
For Each objResult In colQueryResults
Me.lbBox.Items.Add(objResult.Properties("cn")(0))
'Console.WriteLine(objResult.Properties("cn")(0))
Next
End Sub

Private Sub DataGridView1_CellContentClick(ByVal sender As
System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Me.Close()

End Sub

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim objSearch As New DirectorySearcher()
Dim txtSearch As New TextBox
Dim lblListbox As New Label

Me.lblListbox.Text = "Active Users:"
Me.lbBox.Items.Clear()

objSearch.SearchRoot = New DirectoryEntry(Me.txtDomain.Text)
objSearch.Filter =
"(&(objectCategory=person)(objectClass=user)(!user AccountControl:1.2.840.113556.1.4.803:=2))"
objSearch.SearchScope = SearchScope.Subtree
objSearch.PropertiesToLoad.Add("cn")

Dim colQueryResults As SearchResultCollection
colQueryResults = objSearch.FindAll()

Dim objResult As SearchResult
For Each objResult In colQueryResults
Me.lbBox.Items.Add(objResult.Properties("cn")(0))
Next

End Sub

Private Sub txtSearch2_TextChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles txtSearch.TextChanged

End Sub

Private Sub cmdSearch2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdSearch2.Click
Me.lbBox.Items.Clear()
Me.lblListbox.Text = "Search Results:"
Dim objParent As New DirectorySearcher(Me.txtDomain.Text)
Dim txtSearch As New TextBox

If Me.txtSearch.Text = "" Then
MsgBox("Give me something to search")
End

End If

objParent.Filter = "(&(objectCategory=user)(userPrincipalName=" +
Me.txtSearch.Text + "*))"
objParent.PropertiesToLoad.Add("cn")
objParent.PropertiesToLoad.Add("employeeID")
objParent.PropertiesToLoad.Add("employeeNumber")
objParent.PropertiesToLoad.Add("mail")
Dim colQueryResults As SearchResultCollection
colQueryResults = objParent.FindAll()

Dim objResult As SearchResult
For Each objResult In colQueryResults
If objResult.Properties("cn")(0) = "" Then
Me.lbBox.Items.Add("no record found")
End If
Me.lbBox.Items.Add(objResult.Properties("cn")(0))
'Me.lbBox.Items.Add(objResult.Properties("employee ID")(0))
Next
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim objSearch As New DirectorySearcher()
Dim txtSearch As New TextBox
Dim lblListbox As New Label

Me.lblListbox.Text = "Service Accounts:"
Me.lbBox.Items.Clear()

objSearch.SearchRoot = New DirectoryEntry(Me.txtDomain.Text)
objSearch.Filter =
"(&(objectCategory=user)(employeeID=service*)(!emp loyeeNumber=temp))"
objSearch.SearchScope = SearchScope.Subtree
objSearch.PropertiesToLoad.Add("cn")

Dim colQueryResults As SearchResultCollection
colQueryResults = objSearch.FindAll()

Dim objResult As SearchResult
For Each objResult In colQueryResults

Me.lbBox.Items.Add(objResult.Properties("cn")(0))

Next
End Sub

Private Sub cmdEdit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdEdit.Click
Dim lbBox As New ListBox
Dim txtName, txtMail, txtEmployeeID, txtEmployeeNo As New TextBox
Dim objParent As New DirectorySearcher(Me.txtDomain.Text)
'Me.txtName.Text = " "
Dim cmdEdit As New Button

objParent.Filter = "(&(objectCategory=user)(Name=" +
Me.lbBox.SelectedItem + "*))"
objParent.PropertiesToLoad.Add("cn")
objParent.PropertiesToLoad.Add("employeeID")
objParent.PropertiesToLoad.Add("employeeNumber")
objParent.PropertiesToLoad.Add("mail")
Dim colQueryResults As SearchResultCollection
colQueryResults = objParent.FindAll()

Dim objResult As SearchResult
For Each objResult In colQueryResults


'Me.lbBox.Items.Add(objResult.Properties("cn")(0))
'Me.lbBox.Items.Add(objResult.Properties("employee ID")(0))
If colQueryResults.Count = "null" Then

End If
Me.txtName.Text = (objResult.Properties("cn")(0))
Me.txtMail.Text = (objResult.Properties("mail")(0))
Me.txtEmployeeID.Text = (objResult.Properties("employeeID")(0))
Me.txtEmployeeNo.Text =
(objResult.Properties("employeeNumber")(0))

Next
'Me.txtName.Text = " "
'Me.txtName.Text = Me.lbBox.SelectedItem(("Name"(1)))

End Sub

Private Sub lbBox_MouseDoubleClick(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles lbBox.MouseDoubleClick
Dim lbBox As New ListBox
Dim txtName, txtMail, txtEmployeeID, txtEmployeeNo As New TextBox
Dim objParent As New DirectorySearcher(Me.txtDomain.Text)

'Me.txtName.Text = " "
Dim cmdEdit As New Button
If Me.lbBox.SelectedItem = "" Then
MsgBox("Make a selection")
End If
objParent.Filter = "(&(objectCategory=user)(Name=" +
Me.lbBox.SelectedItem + "*))"
objParent.PropertiesToLoad.Add("cn")
objParent.PropertiesToLoad.Add("employeeID")
objParent.PropertiesToLoad.Add("employeeNumber")
objParent.PropertiesToLoad.Add("mail")
Dim colQueryResults As SearchResultCollection
colQueryResults = objParent.FindAll()

Dim objResult As SearchResult
For Each objResult In colQueryResults

'Me.lbBox.Items.Add(objResult.Properties("cn")(0))
'Me.lbBox.Items.Add(objResult.Properties("employee ID")(0))
Me.txtName.Text = (objResult.Properties("cn")(0))
Me.txtMail.Text = (objResult.Properties("mail")(0))
Me.txtEmployeeID.Text = (objResult.Properties("employeeID")(0))
Me.txtEmployeeNo.Text =
(objResult.Properties("employeeNumber")(0))
Next
'Me.txtName.Text = " "
'Me.txtName.Text = Me.lbBox.SelectedItem(("Name"(1)))

End Sub

Private Sub lbBox_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles lbBox.SelectedIndexChanged

End Sub

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

End Sub

Private Sub Button2_MouseDoubleClick(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Button2.MouseDoubleClick

End Sub

Private Sub txtDomain_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles txtDomain.TextChanged

End Sub

Private Sub txtMail_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles txtMail.TextChanged

End Sub

Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdUpdate.Click

End Sub

Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
Dim objSearch As New DirectorySearcher()
Dim txtSearch As New TextBox
Dim lblListbox As New Label

Me.lblListbox.Text = "Service Accounts:"
Me.lbBox.Items.Clear()

objSearch.SearchRoot = New DirectoryEntry(Me.txtDomain.Text)
objSearch.Filter =
"(&(objectCategory=person)(objectClass=user)(userA ccountControl:1.2.840.113556.1.4.803:=2)(employeeI D=Service*)(!employeeNumber=temp))"
objSearch.SearchScope = SearchScope.Subtree
objSearch.PropertiesToLoad.Add("cn")

Dim colQueryResults As SearchResultCollection
colQueryResults = objSearch.FindAll()

Dim objResult As SearchResult
For Each objResult In colQueryResults

Me.lbBox.Items.Add(objResult.Properties("cn")(0))

Next
End Sub

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()
End Sub

Private Sub ActiveUsersToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ActiveUsersToolStripMenuItem.Click
Dim objSearch As New DirectorySearcher()
Dim txtSearch As New TextBox
Dim lblListbox As New Label

Me.lblListbox.Text = "Active Users:"
Me.lbBox.Items.Clear()

objSearch.SearchRoot = New DirectoryEntry(Me.txtDomain.Text)
objSearch.Filter =
"(&(objectCategory=person)(objectClass=user)(!user AccountControl:1.2.840.113556.1.4.803:=2))"
objSearch.SearchScope = SearchScope.Subtree
objSearch.PropertiesToLoad.Add("cn")

Dim colQueryResults As SearchResultCollection
colQueryResults = objSearch.FindAll()

Dim objResult As SearchResult
For Each objResult In colQueryResults
Me.lbBox.Items.Add(objResult.Properties("cn")(0))
Next

End Sub

Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles txtSearch.TextChanged

End Sub

Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdSearch2.Click
Me.lbBox.Items.Clear()
Me.lblListbox.Text = "Search Results:"
Dim objParent As New DirectorySearcher(Me.txtDomain.Text)
Dim txtSearch As New TextBox

If Me.txtSearch.Text = "" Then
MsgBox("Give me something to search")
End

End If

objParent.Filter = "(&(objectCategory=user)(userPrincipalName=" +
Me.txtSearch.Text + "*))"
objParent.PropertiesToLoad.Add("cn")
objParent.PropertiesToLoad.Add("employeeID")
objParent.PropertiesToLoad.Add("employeeNumber")
objParent.PropertiesToLoad.Add("mail")
Dim colQueryResults As SearchResultCollection
colQueryResults = objParent.FindAll()

Dim objResult As SearchResult
For Each objResult In colQueryResults
If objResult.Properties("cn")(0) = "" Then
Me.lbBox.Items.Add("no record found")
End If
Me.lbBox.Items.Add(objResult.Properties("cn")(0))
'Me.lbBox.Items.Add(objResult.Properties("employee ID")(0))
Next
End Sub

Private Sub DisabledUsersToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
DisabledUsersToolStripMenuItem.Click
Dim objSearch As New DirectorySearcher()
Dim lbBox As New ListBox
Dim lblListbox As New Label
Dim txtDomain As New TextBox

Me.lblListbox.Text = "Dislabled Users: "
Me.lbBox.Items.Clear()
objSearch.SearchRoot = New DirectoryEntry(Me.txtDomain.Text)
objSearch.Filter =
"(&(objectCategory=person)(objectClass=user)(userA ccountControl:1.2.840.113556.1.4.803:=2))"
objSearch.SearchScope = SearchScope.Subtree
objSearch.PropertiesToLoad.Add("cn")

Dim colQueryResults As SearchResultCollection
colQueryResults = objSearch.FindAll()

Dim objResult As SearchResult
For Each objResult In colQueryResults
Me.lbBox.Items.Add(objResult.Properties("cn")(0))
'Console.WriteLine(objResult.Properties("cn")(0))
Next
End Sub

Private Sub EnabledToolStripMenuItem1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
EnabledToolStripMenuItem1.Click
Dim objSearch As New DirectorySearcher()
Dim txtSearch As New TextBox
Dim lblListbox As New Label

Me.lblListbox.Text = "Service Accounts:"
Me.lbBox.Items.Clear()

objSearch.SearchRoot = New DirectoryEntry(Me.txtDomain.Text)
objSearch.Filter =
"(&(objectCategory=person)(objectClass=user)(!user AccountControl:1.2.840.113556.1.4.803:=2)(employee ID=Service*)(!employeeNumber=temp))"
objSearch.SearchScope = SearchScope.Subtree
objSearch.PropertiesToLoad.Add("cn")

Dim colQueryResults As SearchResultCollection
colQueryResults = objSearch.FindAll()

Dim objResult As SearchResult
For Each objResult In colQueryResults

Me.lbBox.Items.Add(objResult.Properties("cn")(0))

Next
End Sub
End Class

"Hashim Ismail" wrote:

Hi;
I am quering Active Directory and putting the results of users in the
listbox. Then the user can click on the listbox which should populate
the
textboxes with the fields from AD, which works fine as long as each
field has
a value but i get Index was out of range when one of the fields is
blank. I
am posting sample of the code here:

Dim lbBox As New ListBox
Dim txtName, txtMail, txtEmployeeID, txtEmployeeNo As New
TextBox
Dim objParent As New DirectorySearcher(Me.txtDomain.Text)

'Me.txtName.Text = " "
Dim cmdEdit As New Button
If Me.lbBox.SelectedItem = "" Then
MsgBox("Make a selection")
End If
objParent.Filter = "(&(objectCategory=user)(Name=" +
Me.lbBox.SelectedItem + "*))"
objParent.PropertiesToLoad.Add("cn")
objParent.PropertiesToLoad.Add("employeeID")
objParent.PropertiesToLoad.Add("employeeNumber")
objParent.PropertiesToLoad.Add("mail")
Dim colQueryResults As SearchResultCollection
colQueryResults = objParent.FindAll()

Dim objResult As SearchResult
For Each objResult In colQueryResults

'Me.lbBox.Items.Add(objResult.Properties("cn")(0))
'Me.lbBox.Items.Add(objResult.Properties("employee ID")(0))
Me.txtName.Text = (objResult.Properties("cn")(0))
Me.txtMail.Text = (objResult.Properties("mail")(0))
Me.txtEmployeeID.Text =
(objResult.Properties("employeeID")(0))
Me.txtEmployeeNo.Text =
(objResult.Properties("employeeNumber")(0))
Next

*** Sent via Developersdex http://www.developersdex.com ***

Nov 21 '05 #2

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

Similar topics

14
by: Craig O'Shannessy | last post by:
Hi all, Just thought I'd mention that I really think this problem needs to be fixed. I I'm patching the 7.4RC1 JDBC drivers as we speak due to this optimiser bug, and it's the third time...
29
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array...
2
by: kscdavefl | last post by:
When I run the following code: private void applicationPermissionGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { if (e.Item.ItemType ==...
1
by: Clark Choi | last post by:
I ran the sample application called Petstore from msdn. Everything went fine until I tested Update button on the web form to update the database through Datagrid. I have been searching the web to...
14
by: micklee74 | last post by:
hi say i have string like this astring = 'abcd efgd 1234 fsdf gfds abcde 1234' if i want to find which postion is 1234, how can i achieve this...? i want to use index() but it only give me the...
0
by: ssims | last post by:
I've got a GridView that's sorted by a stored procedure with ROW_NUMBER: PROCEDURE dbo.GetCalendarsByStatusIDPaged ( @startRowIndex int, @maximumRows int, @statusID int ) AS
85
by: Russ | last post by:
Every Python programmer gets this message occasionally: IndexError: list index out of range The message tells you where the error occurred, but it doesn't tell you what the range and the...
2
by: Georgy Panterov | last post by:
I am a relatively new python user. I am writing an economic simulation of acard-game. The simulation runs fine for a few iteration but then it gives an error of "list index out of range." The...
1
by: =?Utf-8?B?SkI=?= | last post by:
Hello As I debug the C# code with a break point and by pressing F11 I eventually get a message stating: ContextSwitchDeadlock was detected Message: The CLR has been unable to transition from...
6
by: shashi shekhar singh | last post by:
Respected Sir, I am facing problem when i try to deploy my website on iis 7.0 on test page. i have to display some .mht files on iframe in gridview and error looks like below, Server Error in...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.