Monthly Archives: March 2013

Difference between Class and Structure


Classes are Reference types and Structures are Values types.

Classes are reference types, basically they will contain the address of an instance variables.

Class SimpleClass
{
Public int Value; 	
}

This the simple class am assigning value to Object to it.

Static Public void Main (string [] arg)
{
SimpleClass objSimpleClass1=new SimpleClass();
objSimpleClass1.Value=5;
SimpleClass objSimpleClass2= new SimpeClass();
objSimpleClass2.Value=10;
}

objSimpleClass1.Value=5

after in next step we change it to 10 in the object objSimpleClass2.  Since both object refer the same address, changes happened in the objSimpleClass2 or in last step that value will reflect in other object also.


			

What is Boxing and UnBoxing


Converting a value type to reference type is called Boxing.

Example:

int i = 12;
object box = i;

Converting a reference type to value type is called UnBoxing.

Example:

object box = 12;
int i = (int)box;

Difference Between StringBuilder and String


 

String:

While concatenate two strings, a new copy in the memory will be created and old string object will be deleted this called ‘IMMUTABLE’. Normally the performance will be slow.
StringBuilder:

 

Normally we use Append() to concatenate the string. combining two string will be take place existing string object. No new memory will be created for this stringBuilder object and its more efficient.

 

 

Enabling and disabling Session


To Optimize the performance once can enable or disable the session in

1) Page Level
2) Application Level

At Page level set the EnableSessionState =”False” in page directive, it will disable the
session activity.

To access the session but not allow to writing data set EnableSessionState =”ReadOnly”

To disable the session in application level, configer the web.config as below

<system.Web>
<Pages enableSessionState=”false”>
</system.Web>

Session State Modes in ASP.NET


Normally You will configure the session in Web.Config or in code behind. In web config session state is configured with the the element <Sessionstate>. In that there are lot of attribute are there like Mode, Timeout, StateConnectionString, CustomProvider, cookieless, cookieName,allowCustomSqlDatabase,useHostingIdentity etc…

ASP.NET session state supports several storage options for session variables and identified
as session-state Mode.

1) Custom
2) InProc
3) Off
4) SQLServer
5) StateServer

The default sessionMode=’InProc’.
a) InProc mode:- which stores session state in memory on the Web server. This is the default.

Note:- If you enable Web-garden mode by setting the webGarden attribute to true in the processModel element of the application’s Web.config file, do not use InProcsession state mode. If you do, data loss can occur if different requests for the same session are served by different worker processes.

b) StateServer mode:- which stores session state in a process, referred to as the ASP.NET state service, that is separate from the ASP.NET worker process or IIS application pool. Using this mode ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm

c) SQLServer mode:- stores session state in a SQL Server database. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm. To use SQLServer mode, you must first be sure the ASP.NET session state database is installed on SQL Server. You can install the ASP.NET session state database using the Aspnet_regsql.exe tool

d) Custom mode:- which enables you to specify a custom storage provider.

e) Off mode:- which disables session state.

More about:- SesionMode Clickhere

Session Event in ASP.NET


There are two types of session events available in ASP.NET:

1) Session_Start
2) Session_End

You can find or handle the above two events in global.asax file. Session_Start event will trigger when ever new session start. Similarly Session_End event trigger when a session is abandoned or expires.

Code to view ViewState information in ASP.NET


It is advisable not to store critical information in ViewState, because it is not encrypted it encoded. To view the information of view state please use the below code.

string strDecodevalue=””;

string strEncodedValue=””; // Copy and past the viewstate encoded value from HTML page.
strDecodevalue = Encoding.ASCII.GetString(Convert.FromBase64String(strEncodedValue ));
MessageBox.Show(strDecodevalue);

How to enable Cookieless Session


By default, the SessionID value is stored in a non-expiring session cookie in the browser. However, you can specify that session identifiers should not be stored in a cookie by setting the cookieless attribute to true in the sessionState section of the Web.config file.

<configuration>
  <system.web>
    <sessionState cookieless="true"
      regenerateExpiredSessionId="true" />
  </system.web>
</configuration>

ASP.NET maintains cookieless session state by automatically inserting a unique session ID into the page’s URL. Then URL will looks like

http://www.kethare.in/(S(lit3py55t21z5v55vlm25s55))/example.aspx

 

Session In ASP.NET


In web when ever webpages request to server entire page will get refreshed, and also since HTTP is stateless protocol we cant maintain store information and all the data will be lost. So how to maintain the information in the stateless environment that where state management come in to picture.

Using Session we can hold the information of the user. Session will be create for each user who accessing the web application. Session object are access with HttpContext.Session property.

Each session have separate sessionID. Client and server communication is happening through sessionID. The data is stored in Session Providers along with the sessionID. If no SessionID value is supplied, ASP.NET starts a new session and the SessionID value for that session is sent to the browser with the response.

By default, SessionID values are stored in a cookie at client side. However, you can also configure the application to store SessionID values in the URL for a “cookie-less” session.

Session will be in active as long as request and response are take place with the same sessionID. If the time between requests for a particular session exceeds the specified time-out value in minutes,the session is considered expired.

Session Variables

Session["FirstName"] = "Kethareeswaran";
Session["LastName"] = "N";

// When retrieving an object from session state, cast it to 
// the appropriate type.
ArrayList stockPicks = (ArrayList)Session["StockPicks"];

// Write the modified stock picks list back to session state.
Session["StockPicks"] = stockPicks;

Feature of IQueryable


1) IQueryable exists in System.Linq Namespace.
2) IQueryable can move forward only over a collection, it can’t move backward and between the items.
3) IQueryable is best to query data from out-memory (like remote database, service) collections.While query data from database, IQueryable execute select query on server side with all filters.
4) IQueryable is suitable for LINQ to SQL queries.
5) IQueryable supports deferred execution.
6) IQueryable supports custom query using CreateQuery and Execute methods.
7) IQueryable support lazy loading. Hence it is suitable for paging like scenarios.
8) Extension methods supports by IEnumerable takes expression objects means expression tree.