Getting number of instances of asp.net website - asp.net

I have an asp.net website and i want to get the number of users currently viewing my site. I am aware that there are some third party softwares available, that would give me the list of the users online but i don't want to do that.
Does anyone know how this can be achieved in asp.net? May be if there are any server variables that would keep a track of the website instances that gives the number of users currently visiting the site. Please help me.

if you want to count users which are using your website at the moment you can use the following code in your global.asax file:
private int activeUsers = 0;
protected void Session_Start(Object sender, EventArgs e)
{
activeUsers++;
Context.Items["activeUsers"] = activeUsers;
}
protected void Application_BeginRequest(Object sender, EventArgs e)
{
Context.Items.Add("activeUsers", activeUsers);
}
protected void Session_End(Object sender, EventArgs e)
{
if(activeUsers > 0)
activeUsers--;
}
protected void Application_End(Object sender, EventArgs e)
{
activeUsers = 0;
}

I would use performance counters instead.
Look under
ASP.NET Application Performance Counters
http://msdn.microsoft.com/en-us/library/fxk122b4.aspx

Related

ASP.NET initialization of array

I have website with array (list) of 1000 objects, these objects are loading from json to array every website refresh. I would like to load these objects from json to array only once and keep it in RAM for others users. Because everytime read file is much slower than read it from RAM.
I am using ASP.NET Web Forms
How is it posssible?
I would recommend to define the array as an static member of a class and then initialize it with help of Global.asax, use the Application_Start event handler.
to add Global.asax to you project in Visual Studio:
File -> New -> File -> Global Application Class
Here is a sample C# code for Global.asax.cs:
public class Global : HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
// ... Your initialization of the array done here ...
}
protected void Session_Start(object sender, EventArgs e)
{
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
protected void Application_Error(object sender, EventArgs e)
{
}
protected void Session_End(object sender, EventArgs e)
{
}
protected void Application_End(object sender, EventArgs e)
{
}
}
Are these values static, i.e., do they stay constant while your application is running? In that case, the easiest way is to cache those values.
You can use static variables for that, but the recommended way is to use the thread-safe Cache object provided by ASP.NET. It can be accessed with the Cache property of the Page or of the HttpContext.
Example:
var myList = (MyListType)Cache["MyList"];
if (myList == null)
{
myList = ...; // Load the list
Cache["MyList"] = myList; // Store it, so we don't need to load it again next time.
}
Further reading:
Caching Application Data

Create specific LoginError asp.net

I'm using the LoginPage by .net 4.5.
When the fields are empty I get an error in red which say I can't connect.
What I want to do, is when I enter username and passwords, I try to log into DB, and if I get false, I want a custom error which says I can't log into DB.
Here is the code:
protected void LoginButton_Click(object sender, EventArgs e)
{
if (!DBHandle.DBConnect(UserLogin.UserName, UserLogin.Password))
{
}
}
protected void UserName_TextChanged(object sender, EventArgs e)
{
}
protected void UserLogin_LoginError(object sender, EventArgs e)
{
UserLogin.FailureText = "asdad";
}
What I have to put in the condition, that cause to get to LoginError?
Thanks!

Routing halts validations' client-side processes. How can I solve this?

I have a trouble with ASP.NET validations. At first my validation worked successfully. But when I began routing, my client-side validations stopped working.
My problem is it deals with braces.
There is successful and unsuccessfull codes:
Successful example
void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add("Pages", new Route("{PageName}/", new PageRouteHandler("~/Page.aspx")));
}
Unsuccessful example
void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add("Pages", new Route("PageName/", new PageRouteHandler("~/Page.aspx")));
}
How can I overcome this problem?

Click event not work properly

In my website I wrote that code:
protected void Page_Load(object sender, EventArgs e){ LinkButton lbtnTopicAddress = new LinkButton(); lbtnTopicAddress.Click += lbtnSpecificTopic1_Click;}
protected void lbtnSpecificTopic1_Click(object sender, EventArgs e){ Server.Transfer("~/SpecificTopic.aspx)"
}
But when I press on the link in run time, the caller doesn't go to the EventHandler method.
Why?
Note,
I wrote code like that in many pages in the same website but it work only in one page.
i added that code to many page in website but it worded only in one page every page has its specific code and no relation between them I hope you understand me thanks
I need help pleaseeeeeeee..........................
Did you mean to miss off a ;and a } here?
protected void lbtnSpecificTopic1_Click(object sender, EventArgs e){ Server.Transfer("~/SpecificTopic.aspx)"
I assume you've put a breakpoint in to ensure it isn't being fired?
I'm not exactly sure but I've got a feeling that instead of Page_Load you need to use Page_Init so your code would look this this:
protected void Page_Init(object sender, EventArgs e)
{
LinkButton lbtnTopicAddress = new LinkButton();
lbtnTopicAddress.Click += lbtnSpecificTopic1_Click;
}
protected void lbtnSpecificTopic1_Click(object sender, EventArgs e)
{
Server.Transfer("~/SpecificTopic.aspx");
}
p.s. 5 mins formatting your code can work wonders when trying to debug
Are you adding the button to the controls on your page, or are you trying to find the "lbtnTopicAddress" control on your page?
Simply declaring the button won't do anything -- you have to get a reference to the control itself from the page.

Link is not working as expectation

I have a user control of menu which have links on which user is redirected to the desired page. When I am using that user control in my aspx page, on first time when user click to the link he is redirected to the destination, but when user again click that link it is showing the page stating that "cannot find server", the same thing is happened to the other links also.
In my user control I am using:
Private void Link1_Click(Object sender, eventargs e)
{
Response.Redirect("Secondpage.aspx");
}
Private void Link2_Click(Object sender, eventargs e)
{
Response.Redirect("Thirdpage.aspx");
}
Unexpectdly, the same code is working fine on the production server but throwing issue in the developmwnt server.
I am not sure the cause of the error.. Thanks in advance.
your event is not found on the server as it is defined as Private
I think Private should be Protected
Protected void Link2_Click(Object sender, eventargs e)
{
Response.Redirect("Thirdpage.aspx");
}

Resources