I have a testing code for AD. It works fine for a while. But next day I run it again and got the error
An exception of type 'System.Runtime.InteropServices.COMException' occurred in System.DirectoryServices.dll but was not handled in user code
The server is not operational.
What's the possible reason? The code is copied from this forum as below:
Dim context As HttpContext = HttpContext.Current
Dim userDS As DataSet = CType(context.Cache(sFilter), DataSet)
If (userDS Is Nothing) Or Not useCached Then
Dim deParent As DirectoryEntry = New DirectoryEntry(path) '''Actually, the error caused at this line
deParent.Username = myusrname
deParent.Password = mypwd
deParent.AuthenticationType = AuthenticationTypes.Secure
Dim ds As DirectorySearcher = New DirectorySearcher(deParent, sFilter, columns, SearchScope.Subtree)
ds.PageSize = 1000
Using (deParent)
userDS = New DataSet("userDS")
Dim dt As DataTable = userDS.Tables.Add("users")
Dim dr As DataRow
For Each prop As String In columns
dt.Columns.Add(CType(prop, String))
Next
Dim src As SearchResultCollection = ds.FindAll() ''''the error was thrown out at this line
Using (src)
For Each sr As SearchResult In src
dr = dt.NewRow()
For Each prop As String In columns
If (sr.Properties.Contains(prop)) Then
dr(prop) = sr.Properties(prop)(0)
End If
Next
dt.Rows.Add(dr)
Next
End Using
End Using
context.Cache.Insert(sFilter, userDS, Nothing, DateTime.MaxValue, TimeSpan.FromSeconds(180))
End If