About the author

Vijay Kodali
E-mail me Send mail

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2017

Web interface for the .NET compiler

Check out compilify.net . It allows you to quickly and easily execute snippets of C# code from your browser.


Categories: C#3.0 | Web Development
Posted by vijay on Thursday, July 19, 2012 4:28 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Asp.net session on browser close

How to capture logoff time when user closes browser?

Or

How to end user session when browser closed?

These are some of the frequently asked questions in asp.net forums.

In this post I'll show you how to do this when you're building an ASP.NET web application.

Before we start, one fact:

There is no full-proof technique to catch the browser close event for 100% of time. The trouble lies in the stateless nature of HTTP. The Web server is out of the picture as soon as it finishes sending the page content to the client. After that, all you can rely on is a client side script. Unfortunately, there is no reliable client side event for browser close.

Solution:

The first thing you need to do is create the web service. I've added web service and named it AsynchronousSave.asmx. 

 Open Dialog

Make this web service accessible from Script, by setting class qualified with the ScriptServiceAttribute attribute... 

clip_image004

Add a method (SaveLogOffTime) marked with [WebMethod] attribute. This method simply accepts UserId as a string variable and writes that value and logoff time to text file. But you can pass as many variables as required. You can then use this information for many purposes.

clip_image006

To end user session, you can just call Session.Abandon() in the above web method.

To enable web service to be called from page’s client side code, add script manager to page. Here i am adding to SessionTest.aspx page

clip_image008

When the user closes the browser, onbeforeunload event fires on the client side. Our final step is adding a java script function to that event, which makes web service calls. The code is simple but effective

clip_image010

My Code

HTML:( SessionTest.aspx )

clip_image012

C#:( SessionTest.aspx.cs )

clip_image014

That’s’ it. Run the application and after browser close, open the text file to see the log off time.

clip_image016

The above code works well in IE 7/8. If you have any questions, leave a comment.


Posted by vijay on Thursday, April 29, 2010 6:09 PM
Permalink | Comments (22) | Post RSSRSS comment feed

Anonymous Types

Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to first explicitly define a type.

 This is an important feature for the LINQ feature that will be integrated into C#. Since anonymous types do not have a named typing, they must be stored in variables declared using the var keyword, telling the C# compiler to use type inference for the variable.


Categories: .Net 3.5 | C#3.0
Posted by vijay on Monday, January 21, 2008 10:57 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Implicitly Typed Local Variables-“Var”

In C# 3.0, you can declare an integer  as –

[code:c#]

var first = 7;

Console.WriteLine("First variable is of type: {0}", first.GetType());

[/code]

It will return”Int32” as DateType, even though we declare it as “var”. So what  is “var”?

from MSDN..

Local variables can be given an inferred "type" of var instead of an explicit type. The var keyword instructs the compiler to infer the type of the variable from the expression on the right side of the initialization statement.

To put it simply "var" is a keyword that results in variable being of the same data type, of the initializer. It works similar to “object” type in older version. But there is great difference between “Object” and “var”. Check following code snippet-

[code:c#]

object newObj = 58;  

int test = newObj; //this line result in an error "Cannot implicitly convert type object to int"

 int test = (int)newObj; // this line will work. But result in boxing..additional overhead

[/code]

So Objects have boxing and unboxing issues. Now consider same instance with “var”- 

[code:c#]

var newObj = 22;

int test = newObj; //no boxing involved. It is type-safe

[/code]

C# is still strongly typed. “Var” is NOT a variant, everything is known by the compiler at compile-time.

Tags:
Posted by vijay on Friday, January 18, 2008 9:11 PM
Permalink | Comments (0) | Post RSSRSS comment feed