Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey all,

I´m getting an Exception in a method, but i don´t know why...
it is only thrown when the input of the TextBox could not be found, but I think I´m checking against it. Perhaps wrong thinking?


public String getUser()
        {
            String user = User.Identity.Name.Split(new Char[] { '\\' })[1];
            if (TextBox_Admin.Visible && TextBox_Admin.Text.Length > 0)
            {
                DirectorySearcher searcher = new DirectorySearcher("(&(name=" + TextBox_Admin.Text.ToString() + ")(objectClass=user))");
                searcher.SearchRoot = entiry;
                searcher.PropertiesToLoad.Add("cn");
                SearchResult res = searcher.FindOne();
                
                if (res.Properties.Contains("cn") && res.Properties.Equals(TextBox_Admin.Text))//at this point the Exception is thrown
                    user = TextBox_Admin.Text.ToString();
                else
                {
                    Label_Admin.Text = "Unknown User";
                    user = null;
                }
            }  
            return user;
        }


NullReferenceException:
Object reference not set to an instance of an object.
Can someone help?



Ok, found it ;)
Posted
Updated 29-Sep-11 1:23am
v2

1 solution

Determine what is null in the debugger! Probably 'res' in this case, which indicates the FindOne() method didn't return anything.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900