Month: October 2014

Set Default value for property with Setter and Getter C#

We often use setters and getters to control the access of our properties. But sometimes we want to define the default value of that property and we cannot do that the normal way. To accomplish this we have to add the DefaultValue Attribute for that property.

[DefaultValue("Tom Hanks")]
public string Name { get; set; }
Advertisements

Configuring ASP session state on SQL server

This is a brief to the point note on how to setup ASP session state to use SQL server to store session information.

  1. Open Visual Studio CMD and run the following command.
    For SQL Server with Windows Authentication Security:

    aspnet_regsql -S 'serverName' -E -ssadd -sstype p

    For SQL Server with SQL Security:

    aspnet_regsql -S 'serverName' -U 'UserName' -P 'Password' -ssadd -sstype p

    1
    This command will create the ASP Session State tables in the SQL Server. You will find in the figure above that i have used ‘.’ in the ServerName as i am pointing to the Local SQL Server on my machine.
    If you don’t have visual studio CMD you can still open the normal CMD and navigate to the .net framework “Cd ‘C:\WINDOWS\Microsoft.NET\Framework\v4.XXX’ ” then use the aspnet_regsql

  2. Now after having our tables ready we need to reconfigure our web.config to change the session mode to SQLServer.
    For SQL Server with Windows Authentication Security:

    <sessionstate mode="SQLServer" timeout="20" allowcustomsqldatabase="true" 
    sqlconnectionstring="Data Source=Server;Integrated-Security=SSPI;" cookieless="false">

    For SQL Server with SQL Security:

    <sessionstate mode="SQLServer" timeout="20" allowcustomsqldatabase="true" 
    sqlconnectionstring="Data Source=Server;User ID=UserID;Password=Password;" 
    cookieless="false">

That’s it, Now you have your ASP session stored in you SQL server 🙂

Run Code on Another Thread , C#

When we have a certain function that takes a long time for example uploading a file to the server, we don’t want the user wait for that function to finish. We can instead run that function in the background on another thread.

The easiest way of running a function in the background is using ThreadPool.QueueUserWorkItem.

System.Threading.ThreadPool.QueueUserWorkItem(delegate {
   // Write your Code that will run in background here 
}, null);

Allow Multiple Remote Sessions on Windows Server

By default Windows server allows only a single session to be connected remotely to the server. You can change that by allowing two session to be connected on the same time.

1) Press the Windows Key + R then type MMC.exe

2) File -> Add/Remove Snap-in -> click on Group Policy Object -> Add -> Finish -> OK

3) Double click Computer Configuration -> Administrative Templates -> Windows Components -> Remote Desktop Services -> Remote Desktop Session Host -> Connections.

4) Set the Limit Number of Connections to 999999

5) Restrict Remote Desktop Services users to a single Remote Desktop Services session = Disabled

Now you can access the Server remotely with two Sessions 🙂