Wednesday, 21 August 2013

Avoid sharing of same session across multiple tabs using

By default all browsers share session state between multiple tabs. So if you logged into one tab with particular site and open internal link of the same site in new tab, you need not to worry to login again. It will automatically declare you as logged in user.

When open different tabs, those tab will share same session if user access same web app in different tabs. same as sign out process also.

this is normal behavior of browser (shared single session on multiple tab).

Tab based browser – session handling

if you need each tab will get unique ID and it will looks like it is another visitor, try this code

    <sessionState cookieless="true"
      regenerateExpiredSessionId="true" />

Wednesday, 10 July 2013

What is the difference between a field and a property in C#?

Fields and properties look the same, but they are not. Properties are methods and as such there are certain things that are not supported for properties, and some things that may happen with properties but never in the case of fields.

Here's a list of differences:

  • Fields may be used as input to out/ref arguments. Properties may not.

  • A field will always yield the same result when called multiple times (if we leave out issues with multiple threads). A property such as DateTime.Now is not always equal to itself.

  • Properties may throw exceptions - fields will never do that.

  • Properties may have side effects or take a really long time to execute. Fields have no side effects and will always be as fast as can be expected for the given type.

  • Properties support different accessibility - fields do not (but fields can be made readonly)

  • When using reflection the properties and fields are treated as different MemberTypes so are located differently (GetFields vs GetProperties for example)

  • The JIT Compiler may treat property access very differently to field access. It may however compile down to identical native code but the scope for difference is there.

  • An important difference is that interfaces can have properties but not fields

  • Properties may run for a very long time, have side effects, and may even throw exceptions. Fields are fast, with no side effects, and will never throw exceptions. Due to side effects a property may return a different value for each call (as may be the case for DateTime.Now, i.e. DateTime.Now is not always equal to DateTime.Now). Fields always return the same value.

  • Fields may be used for out / ref parameters, properties may not. Properties support additional logic – this could be used to implement lazy loading among other things.

  • Using Properties, you can throw an event, when the value of the property is changed (aka. PropertyChangedEvent) or before the value is changed to support cancelation.

  • public class Person {
     private string _name;
     public event EventHandler NameChanging;    
     public event EventHandler NameChanged;
     public string Name{
         return _name;
         _name = value;
     private void OnNameChanging(){
       EventHandler localEvent = NameChanging;
       if (localEvent != null) {
     private void OnNameChanged(){
       EventHandler localEvent = NameChanged;
       if (localEvent != null) {

  • In the background a property is compiled into methods. So a Name property is compiled into get_Name() and set_Name(string value). You can see this if you study the compiled code. So there is a (very) small performance overhead when using them. Normally you will always use a Property if you expose a field to the outside, and you will often use it internally if you need to do validation of the value.

The remote server returned an error: (500) Internal Server Error

When the remote server returns an error, then it means that the remote server noticed something bad. When the error is 500, that means it's an internal error, meaning internal to the service - the service threw an exception that was not caught.

The remote server don't return the correct HTML, so it throw errors.

Check Windows event viewer logs you might get more information on the reason of the 500 error.

Thursday, 16 May 2013

How to know whether record is updated or not

When you do an update, it's immediately return rows updated: It Return Number of Records Affected

@@ROWCOUNT - Returns the number of rows affected by the last statement and Return type of @@ROWCOUNT is int.

The following example executes an UPDATE statement and uses @@ROWCOUNT to detect if any rows were changed

UPDATE Tbl_EmpProfile  SET JobTitle = 'Executive' WHERE EMPID = 5001
PRINT 'Warning: No rows were updated';

You can set this @@rowcount to some other variable and use them for further processing.


UPDATE Tbl_EmpProfile  SET JobTitle = 'Executive' WHERE EMPID = 5001
SET @rows = @@rowcount

using Stored Procedure, Register an out parameter for the stored procedure, and set the value based on @@ROWCOUNT if using SQL Server. Use SQL%ROWCOUNT if you are using Oracle.

if you have multiple INSERT/UPDATE/DELETEs, you'll need a variable to store the result from @@ROWCOUNT for each operation.

Friday, 10 May 2013

Maintain the check marks in checkboxlists using session

To specify items that you want to appear in the CheckBoxList control, place a ListItem element for each entry between the opening and closing tags of the CheckBoxList control.

The CheckBoxList control also supports data binding. To bind the control to a data source, first create a data source, such as one of the DataSourceControl objects, that contains the items to display in the control. Next, use the DataBind method to bind the data source to the CheckBoxList control. Use the DataTextField and DataValueField properties to specify which field in the data source to bind to the Text and Value properties of each list item in the control, respectively. The CheckBoxList control will now display the information from the data source.

<asp:CheckBoxList ID="CheckBoxList1" runat="server">
    <asp:ListItem Text="Item 1" Value="1"></asp:ListItem>
    <asp:ListItem Text="Item 2" Value="2"></asp:ListItem>
    <asp:ListItem Text="Item 3" Value="3"></asp:ListItem>

To determine the selected items in the CheckBoxList control, iterate through the Items collection and test the Selected property of each item in the collection.

int count = CheckBoxList1.Items.Count;
ArrayList values = new ArrayList();

 for (int i = 0; i < count; i++)
               if (CheckBoxList1.Items[i].Selected)

//save selected checkboxes in session state
Session["CheckBoxList"] = values;

//casting the value from the Session
ArrayList values = (ArrayList)Session["CheckBoxList"];

foreach (ListItem li in CheckBoxList1.Items)
                if (values.Contains(li.Value))
                    li.Selected = true;
                    li.Selected = false;

Friday, 3 May 2013

How to handle multiple data tables using Data Reader in ASP.NET

This is best used when you just want to fetch data in read only mode , populate your business entity and close the reader. This is really fast. But you cannot do update with data reader.

Data reader can hold data from multiple tables and data reader can hold more than one table.

If you want to Read/Update the data, You can Read/Update the data with data adapters but it is less faster when reading the data then Data reader.

When you read the results of a batch SQL statement, you can use the NextResult method to position the DataReader at the next result in the result set.

do check like


By default, the data reader is positioned on the first result.

What is Static Class in C#?

If a class declaration includes the keyword static then that class is termed as static class. Static class is a specialized class that cannot have any objects.

First of all, the difference between a non-static and a static class in C# is simply that a static class cannot be instantiated. This means that you don’t use the new keyword to instantiate an instance of the class. Instead, you will access members of the static class by using the class name. Basically, a static class is intended to be a container for a set of methods that only work with supplied parameters and don’t have a need to store or retrieve data that is unique to them.

there are two main features of a static class, one is no object of static class can be created and another is, a static class must contain only static members, then it is important that what is the main benefit to create a static class, the main benefit of making static class, we do not need to make any instance of this class ,all members can be accessible with its own name.

Static Members
A static method, field, property, or event is callable on a class even when no instance of the class has been created. If any instances of the class are created, they cannot be used to access the static member. Only one copy of static fields and events exists, and static methods and properties can only access static fields and static events. Static members are often used to represent data or calculations that do not change in response to object state; for instance, a math library might contain static methods for calculating sine and cosine.

Static class members are declared using the static keyword before the return type of the member, for example:

public class Automobile
    public static int NumberOfWheels = 4;
    public static int SizeOfGasTank
            return 15;
    public static void Drive() { }
    public static event EventType RunOutOfGas;

    //other non-static fields and properties...

Static members are initialized before the static member is accessed for the first time, and before the static constructor, if any is called. To access a static class member, use the name of the class instead of a variable name to specify the location of the member. For example:

int i = Automobile.NumberOfWheels;

Characteristics of Static Class in C#
  • Members of Static Class Can Only Be Static
  • Static Class Can Contain Only Static Constructor
  • Static Class is Implicitly Sealed
  • Static Class Cannot Be Declared as Sealed
  • Static Class Cannot be Declared as Abstract
  • Static Class Should Inherit Only Object
  • Static Class Cannot be Inherited

When to Use Static Classes
Suppose you have a class CompanyInfo that contains the following methods to get information about the company name and address.

class CompanyInfo
    public string GetCompanyName() { return "CompanyName"; }
    public string GetCompanyAddress() { return "CompanyAddress"; }


These methods do not need to be attached to a specific instance of the class. Therefore, instead of creating unnecessary instances of this class, you can declare it as a static class, like this:

static class CompanyInfo
    public static string GetCompanyName() { return "CompanyName"; }
    public static string GetCompanyAddress() { return "CompanyAddress"; }

Use a static class as a unit of organization for methods not associated with particular objects. Also, a static class can make your implementation simpler and faster because you do not have to create an object in order to call its methods. It is useful to organize the methods inside the class in a meaningful way, such as the methods of the Math class in the System namespace.