Finding no of users online ASP.NET

To do this,
1) Add a new item global.asax in your website.
2) Put the following code in it

void Application_Start(object sender, EventArgs e)
// Code that runs on application startup
Application[“UsersOnline”] = 0;

void Session_Start(object sender, EventArgs e)
// Code that runs when a new session is started
Application[“UsersOnline”] = (int)Application[“UsersOnline”] + 1;

void Session_End(object sender, EventArgs e)
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
Application[“UsersOnline”] = (int)Application[“UsersOnline”] – 1;

3) In the webpage where the no of online users have to be displayed make use of this application object

protected void Page_Load(object sender, EventArgs e)
Response.Write(“The no of users online are ” + Application[“UsersOnline”].ToString());

That’s it

Searching for an email id in Active Directory using C#

These are the steps i followed
1) Created a new windows application.
2) Added reference to System.DirectoryServices dll
3) Added a textbox(where the email id will be entered) and a button in the form.
4) On btnClick this is the code i wrote

we use the DirectorySearcher class to query the directory and read the attributes of the objects found
DirectorySearcher Dsearch = new DirectorySearcher();
EmailID = txtEmailID.Text;

The filter in an LDAP query restricts the objects that the search will return. It is the equivalent of the WHERE clause

Dsearch.Filter = “(&(objectCategory=person)(sAMAccountName=*)(mail=”+EmailID+”))”;
objectCategory=This attribute is meant to be used to group common types of objects together so that we can search across all of them. Both user and contact classes share the same objectCategory value of person.
sAMAccountName =To separate users from contacts efficiently we can make user of sAMAccountName attribute
mail =Finally the mail attribute is the attribute which we looking for. Setting it to the value entered in the textbox by the user

SearchResult sResult = Dsearch.FindOne();

A SearchResult represents a single item from a result set.The FindOne method returns a single SearchResult, representing the first result in the underlying result set. Internally, this method is calling the FindAll method and returning the first SearchResult from the SearchResultCollection

if (sResult != null)
txtInfo.Content = “This email id already existing”;
txtInfo.Content = “This email id is not found”;

That’s it.