473,508 Members | 2,130 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DirectorySearcher.FindAll() causes Unspecified Error in C# but not in VB.NET

Hi

All C# ADSI samples that I run cause unspecified errors. If I
translate them into VB.NET they run fine.

Here's an example:

public string getEmail(string LDAPPath, string username)
{
DirectoryEntry entry = new DirectoryEntry(LDAPPath);

DirectorySearcher mySearcher = DirectorySearcher(entry);

mySearcher.PropertiesToLoad.Add("mail");
mySearcher.Filter =
("&(objectClass=user)(sAMAccountName="+username+") ");

StringBuilder sb = new StringBuilder();

foreach (SearchResult result in mySearcher.FindAll())
sb.Append(result.Properties["0"].ToString());

return(sb.ToString());
}

results in:

Unspecified error
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.Runtime.InteropServices.COMException:
Unspecified error

Source Error:
Line 41: StringBuilder sb = new StringBuilder();
Line 42:
Line 43: foreach (SearchResult result in mySearcher.FindAll())
Line 44: sb.Append(result.Properties["0"].ToString());
Line 45:
Source File: c:\inetpub\wwwroot\area51\adsi\webform6.aspx.cs Line:
43

Stack Trace:
[COMException (0x80004005): Unspecified error]
System.DirectoryServices.DirectoryEntry.Bind(Boole an throwIfFail)
+704
System.DirectoryServices.DirectoryEntry.Bind() +10
System.DirectoryServices.DirectoryEntry.get_AdsObj ect() +10
System.DirectoryServices.DirectorySearcher.FindAll (Boolean
findMoreThanOne) +198
System.DirectoryServices.DirectorySearcher.FindAll () +10
area51.ADSI.WebForm6.getEmail(String LDAPPath, String username) in
c:\inetpub\wwwroot\area51\adsi\webform6.aspx.cs:43
area51.ADSI.WebForm6.Page_Load(Object sender, EventArgs e) in
c:\inetpub\wwwroot\area51\adsi\webform6.aspx.cs:28
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +750

I'm using "<identity impersonate="true" />" in the web config file.
Any ideas why I can use DirectorySearcher from VB but not from C#?
Nov 16 '05 #1
5 24676
>public string getEmail(string LDAPPath, string username)
{
DirectoryEntry entry = new DirectoryEntry(LDAPPath);

DirectorySearcher mySearcher = DirectorySearcher(entry);

mySearcher.PropertiesToLoad.Add("mail");
mySearcher.Filter =
("&(objectClass=user)(sAMAccountName="+username+" )");

StringBuilder sb = new StringBuilder();

foreach (SearchResult result in mySearcher.FindAll())
sb.Append(result.Properties["0"].ToString());


First of all, it should be

sb.Append(result.Properties[0].ToString());

If you have the 0 in double quotes, you're trying to access a property
by the name of "0" which doesn't exist, so you get back a NULL value,
which you try to cast by calling .ToString() - but you're calling that
on a NULL value, hence the error.

Also - if that particular user's "mail" attribute is NOT set, then
you'll get back a NULL value for the .Properties[0], too, which you
can't just call a .ToString() on either.

VB.NET probably shields you from those programming errors by some
means of a default behaviour - in C#, you need CHECK for those NULL
values!

Marc
================================================== ==============
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
Nov 16 '05 #2
Thanks for the response Mark. I found a stupid error in the LDAP
string I was passing where I wasn't appending "LDAP":

DirectoryEntry oRootDSE = new DirectoryEntry("LDAP://RootDSE");
string LDAPPath = oRootDSE.Properties["defaultNamingContext"][0].ToString();

which I fixed. But I missed the double quotes and null possibility.

if (result.Properties["mail"][0] != null)
sb.Append(result.Properties["mail"][0].ToString());

....now works, thanks again.
If you have the 0 in double quotes, you're trying to access a property
by the name of "0" which doesn't exist, so you get back a NULL value,
which you try to cast by calling .ToString() - but you're calling that
on a NULL value, hence the error.

Also - if that particular user's "mail" attribute is NOT set, then
you'll get back a NULL value for the .Properties[0], too, which you
can't just call a .ToString() on either.

VB.NET probably shields you from those programming errors by some
means of a default behaviour - in C#, you need CHECK for those NULL
values!

Marc
================================================== ==============
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch

Nov 16 '05 #3
>which I fixed. But I missed the double quotes and null possibility.

if (result.Properties["mail"][0] != null)
sb.Append(result.Properties["mail"][0].ToString());


Actually, I almost think you'd have to change this to:

if (result.Properties["mail"] != null)
sb.Append(result.Properties["mail"][0].ToString());

You see - if the "mail" attribute is not set, then the
..Properties["mail"] is already null, and indexing that by [0] will
cause an exception.

Marc

================================================== ==============
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
Nov 16 '05 #4
Jay
I have been searching for the solution to the same error,
but I think it may be related to the DirectoryEntry()
call.

When I explicitly specify the DirectoryEntry parameters
it works fine, when I try to use a variable (LDAPQuery)
it fails. The string is being created properly as I can
assign it to a label and have it output correctly.

Any clues?
Code:

// Create AD LDAP search objects
LDAPQuery = string.Format("\"LDAP://{0}\", \"{1}\", \"{2}
\"", domain, UsrName, UsrPass);

// This does not work
DirectoryEntry SearchRoot = new DirectoryEntry(LDAPQuery);

// This works
// DirectoryEntry SearchRoot = new DirectoryEntry
("LDAP://testdomain", "fubarjones", "Testing@9");

Jay

-----Original Message-----
public string getEmail(string LDAPPath, string username){
DirectoryEntry entry = new DirectoryEntry (LDAPPath);
DirectorySearcher mySearcher = DirectorySearcher (entry);
mySearcher.PropertiesToLoad.Add("mail");
mySearcher.Filter =
("&(objectClass=user)(sAMAccountName="+username+ ")");

StringBuilder sb = new StringBuilder();

foreach (SearchResult result in mySearcher.FindAll ()) sb.Append(result.Properties["0"].ToString
());
First of all, it should be

sb.Append(result.Properties[0].ToString ());
If you have the 0 in double quotes, you're trying to access a propertyby the name of "0" which doesn't exist, so you get back a NULL value,which you try to cast by calling .ToString() - but you're calling thaton a NULL value, hence the error.

Also - if that particular user's "mail" attribute is NOT set, thenyou'll get back a NULL value for the .Properties[0], too, which youcan't just call a .ToString() on either.

VB.NET probably shields you from those programming errors by somemeans of a default behaviour - in C#, you need CHECK for those NULLvalues!

Marc
================================================= ======== =======Marc Scheuner May The Source Be With You!Bern, Switzerland m.scheuner(at) inova.ch.

Nov 16 '05 #5
>When I explicitly specify the DirectoryEntry parameters
it works fine, when I try to use a variable (LDAPQuery)
it fails. The string is being created properly as I can
assign it to a label and have it output correctly. // Create AD LDAP search objects
LDAPQuery = string.Format("\"LDAP://{0}\", \"{1}\", \"{2}
\"", domain, UsrName, UsrPass);
What EXACTLY is in the LDAPQuery string in the end here???
// This does not work
DirectoryEntry SearchRoot = new DirectoryEntry(LDAPQuery);


If it doesn't work, then most likely your LDAP string is invalid /
incomplete.

Marc

================================================== ==============
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
Nov 16 '05 #6

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

Similar topics

3
8486
by: Andrej | last post by:
hi, i must skip throug all of my contacts in AD and check if targetAddress = entry in the array of ProxyAddress. DirectorySearcher.FindAll() methode returns just 1000 elemets, but i have much...
1
1764
by: Hin | last post by:
I often encounter the following when i try to view the form in VS.NET 2003: An error occurred while loading the document. Fix the error, and then try loading the document again. The error message...
4
2524
by: RM | last post by:
Had VS .Net 2002 installed on W2k Server SP3 and supported a number of web sites. Installed VS .Net 2003 on Friday and now all web sites using .Net & MS ACCESS get this strange error upon open. ...
2
2642
by: Jim Lacenski | last post by:
I have a VB class that uses .NET and ADODB to write into an Excel spreadsheet (via Jet) on a server as part of a web application. ADODB is used instead of ADO.NET because it greatly simplifies the...
1
6805
by: T8 | last post by:
I have a asp.net (framework 1.1) site interfacing against SQL 2000. It runs like a charm 99% of the time but once in a while I get the following "unspecified error". Sometimes it would resolve by...
3
1913
by: GotdotNet | last post by:
I get this error once in a while and I normally could fix it by restarting IIS. However, I would like to find out what causes this. Can anyone help? -----------------------------error...
1
3216
by: Siegfried Heintze | last post by:
I'm using a third party hosting service. I presently have a Web Service on this hosting service's server that loads and executes a native mode DLL. This demonstrates that the hosting service has...
4
2978
by: sjoshi | last post by:
Hello All I'm trying this to filter group users bu tI keep getting an unspecified error when invoking FindOne method. Any help is greatly appreciated. public static DirectoryEntry...
0
1625
by: Jonathan | last post by:
Hi Everyone, I have a problem which I've been able to reproduce with a nice and small test ASP.NET web application. The page sets the window.onbeforeunload event so that we will get an Ok/Cancel...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7383
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
7498
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3194
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1557
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.