Monday, January 30, 2012

Aspnet_Regiis Utility

Aspnet_Regiis Utility
It is the IIS registration tool that is used to register the ASP.Net with IIS. It will update the script map of an application to the coresponding ISAPI extensions to process the request given to the IIS. For example, “aspx” extension will be mapped to aspnet_isapi.dll and hence request for an aspx page to IIS will be given to aspnet_isapi. Refer the below figure for a sample script map.
You can bring the script map by right clicking Root directory or a website >  property > Click Configuration from “Home Directory” tab.

 This is the tool that is used to configure different versions of to run side by side in the same web server.
One thing to note here is, each version of framework will have its own Aspnet_Regiis utility packed in it i.e. if there is 1.1 and 2.0 framework installed on the box then each version will have its own Aspnet_Regiis utility.

For example, 1.1 framework utility will be in,


For example, 2.0 framework utility will be in,


A Common Solution with Aspnet_Regiis utility
Executing this utility with –i switch will be a common solution for number of issues we face. One such example will be the following error,
The XML Page cannot be displayed.
Cannot view XML input using XSL style sheet.

Refer the below figure.

The above command will update the script map of the root directory of the web server and hence making all the website’s script map under the root updated with the current version. Since we have more than one version of ASP.Net (1.1,2.0 etc) there are possibilities that the web server may have websites running on different version of The above command will update the script map of all the websites and thus making the other websites settings running in different version disturbed.

Configuring ASP.Net Side by Side Execution Using Aspnet_Regiis Utility
To achieve this we need to update the script map specific to the application/website instead of root level. It can be done by using “-s” switch.

Aspnet_regiis –s path

Aspnet_regiis –s W3SVC/1/ROOT/CodeDigest

Where CodeDigest is the application or virtual directory whose script map has to be updated. Thus, the other websites configurations are intact.

So to register 1.1 framework,

C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322>Aspnet_regiis -s W3SVC/1/ROOT/CodeDigest

For 2.0 framework,

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>Aspnet_regiis -s W3SVC/1/ROOT/CodeDigest

Thursday, January 26, 2012

Using SQLite in your C# Application


SQLite is a small, fast and embeddable database where the database engine and the interface are combined into a single library. It also has the ability to store all the data in a single file. So if your application requires a standalone database, SQLite is perhaps the perfect choice for you. There are, of course, other reasons for choosing SQLite including:
  • SQLite has a small memory footprint and only a single library is required to access databases, making it ideal for embedded database applications.
  • SQLite has been ported to many platforms and runs even on Windows CE and Palm OS.
  • SQLite is ACID-compliant, meeting all four criteria - Atomicity, Consistency, Isolation, and Durability.
  • SQLite implements a large subset of the ANSI-92 SQL standard, including views, sub-queries and triggers.
  • No problem of extra database drivers, ODBC configuration required. Just include the library and the data file with your application.
  • SQLite has native language APIs for C/C++, PHP, Perl, Python, Tcl etc. Native API for C# is still not present.

Using the Code

To use SQLite in your C# application, you need to download a third party free ADO.NET data provider ( from here.
Screenshot - Executable
Fig 1: Demo C# SQLite application.
Our demo application uses SQLite database to store id and description value pairs in a single table – mains. We will display the contents of this table in a grid with added functionalities of adding/deleting and modifying records to and from the table. To set up the system, please follow the steps below:
  1. Unzip the archive, and place the *.dll files into the Binary folder.
  2. Use the SQLite.NET.dll in your .NET application by ‘Project -> Add Reference’.
  3. Add Using Finisar.SQLite; in the using directive declaration region of your *.cs file.
  4. Declare the following private fields to be used in your application:
    private SQLiteConnection sql_con;
    private SQLiteCommand sql_cmd;
    private SQLiteDataAdapter DB;
    private DataSet DS = new DataSet();
    private DataTable DT = new DataTable();
  5. Create a function to set up the Connection String. DemoT.db is the name of the single data file. It already has a single table – mains. Version (=3) is the version of the database engine.
    private void SetConnection() 
    sql_con = new SQLiteConnection
     ("Data Source=DemoT.db;Version=3;New=False;Compress=True;"); 
  6. Create a generic function ExecuteQuery, to execute Create Command queries.
    private void ExecuteQuery(string txtQuery) 
    sql_cmd = sql_con.CreateCommand(); 
  7. Now create a LoadData function to access the SQLite database and retrieve the data from the mains table and fill the Dataset. Please refer to the following code:
    private void LoadData() 
    sql_cmd = sql_con.CreateCommand(); 
    string CommandText = "select id, desc from mains"; 
    DB = new SQLiteDataAdapter(CommandText,sql_con); 
    DT= DS.Tables[0]; 
    Grid.DataSource = DT; 
  8. To add/edit/delete an entry to and from the table, just pass the required query to the already created ExecuteQuery function. Please see the following code for an example:
    private void Add()
    string txtSQLQuery = "insert into  mains (desc) values ('"+txtDesc.Text+"')";
To administer SQLite database, an open-source SQLite GUI Database Browser utility is of much use. It can be downloaded from here.


So should SQLite be used for all database driven applications? No.
Like all databases, SQLite has its list of shortcomings. It is not suitable for a client server application or as a networked database. It’s not suited well for a multi user scenario and can have serious file locking issues when accessed simultaneously over the network. Quite for the same reason, SQLite is not suited for a multi-threaded or a multi-process application-database access scenario.
To conclude, SQLite is very suitable for memory constraint systems like WinCE, Palms, Smart Phones, embedded devices and also normal single user desktop applications where its small memory footprint, single library and its copy and paste deployment feature give it a distinct advantage.

Tuesday, January 17, 2012

Simple Example of NHibernate and SQL Server 2008

In this article, i will create a simple application that implent NHibernate, and in database side, i will use SQL Server 2008. What is NHibernate ? I will not explain it in there, so many explanation what it is in internet, like in here.
You can download latest NHibernate version here.
First, i will create a simple database that have just one table (and i added some record in there):
This is the DDL
1create table ACCOUNT (
2   ACCID                int         identity(1,1)       not null,
3   USERNAME             varchar(50)                     null,
4   PASS                 varchar(50)                     null,
5   constraint PK_ACCOUNT primary key nonclustered (ACCID)
Hmm.. what is Identity(1,1)  mean ??? It is mean we will set ACCID attribute, the primary key, to become autonumbered, the value will be added automatically incrementing from 1, 2, 3, … and so on.
Next step, we will create simple solution in Visual Studio 2008 SP1 (remember … to connect to SQL Server 2008, your Visual Studio 2008 must be Service Pack 1 ).

Add some references
  1. NHibernate.dll (NHibernate library)
  2. NHibernate.ByteCode.LinFu.dll (for lazy loading)
  3. System.configuration

Test your connection with database using Server Explorer

Do not forget to copy the connection string…

For my example, my database connection string is :  ]
Data Source=WIRTH;Initial Catalog=TestNHibernate;User ID=azer89;Password=azer89
Nexttt !!!! create an App.config file in your project (Just Right click your project, Add, New Item, and Application Configuration File)

Set your App.config like this :
01xml version="1.0" encoding="utf-8" ?>
03  <configSections>
04    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
05  configSections>
06  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
07    <session-factory>
08      <property name="connection.provider">NHibernate.Connection.DriverConnectionProviderproperty>
09      <property name="connection.driver_class">NHibernate.Driver.SqlClientDriverproperty>
10      <property name="connection.connection_string">Data Source=WIRTH;Initial Catalog=TestNHibernate;User ID=azer89;Password=azer89property>
11      <property name="dialect">NHibernate.Dialect.MsSql2008Dialectproperty>
12      <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFuproperty>
13      <mapping assembly="NHibernateTest"/>
14    session-factory>
15  hibernate-configuration>
You must pay attention in property connection.connection_string and dialect in code above. Add a model class similar to Account Table, its fields must be similar too

Your Account.cs should be like this :
01using System;
02using System.Collections.Generic;
03using System.Linq;
04using System.Text;
06namespace NHibernateTest
08    class Account
09    {
10        private int accId;
11        private string username;
12        private string pass;
14        public virtual int AccId
15        {
16            get { return accId; }
17            set { accId = value; }
18        }
20        public virtual string UserName
21        {
22            get { return username; }
23            set { username = value; }
24        }
26        public virtual string Pass
27        {
28            get { return pass; }
29            set { pass = value; }
30        }
31    }
After creating Account class, we will create map to Account Table using XML file, so NHibernate will know that Account class in application will map to Account Table in database. The XML filename will be “Account.nbm.xml”
01xml version="1.0" encoding="utf-8" ?>
02<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateTest" assembly="NHibernateTest">
03  <class name="Account" table="ACCOUNT" lazy="false">
04    <id name="AccId">
05      <column name="ACCID"/>
06      <generator class="native"/>
07    id>
08    <property name="UserName">
09      <column name="username"/>
10    property>
11    <property name="Pass">
12      <column name="pass"/>
13    property>
14  class>
Look at generator class, its value is “native”, it is because we have set ACCID column into autoincrement. Oh yeah, Build Action for hbm.xml file must be “Embedded Resource”

Next step is to create persistence class, we will name it NHibernateHelper.cs
01using System.Reflection;
02using NHibernate;
03using NHibernate.Cfg;
05namespace NHibernateTest
07    public sealed class NHibernateHelper
08    {
09        private static ISessionFactory SessionFactory;
11        private static void OpenSession()
12        {
13            Configuration configuration = new Configuration();
14            configuration.AddAssembly(Assembly.GetCallingAssembly());
15            SessionFactory = configuration.BuildSessionFactory();
16        }
18        public static ISession GetCurrentSession()
19        {
20            if (SessionFactory == null)
21                NHibernateHelper.OpenSession();
23            return SessionFactory.OpenSession();
24        }
26        public static void CloseSessionFactory()
27        {
28            if (SessionFactory != null)
29                SessionFactory.Close();
30        }
31    }
Since ISessionFactory is expensive to create (it almost take one second in my computer !!!) we will create one object of ISessionFactory, otherwise with ISession, it is very cheap to create, so we can create it very ofter in your application. We will test the code :
01using System;
02using System.Collections.Generic;
03using System.Linq;
04using System.Text;
05using NHibernate;
07namespace NHibernateTest
09    class Program
10    {
11        static void Main(string[] args)
12        {
13            string username = "reza";
14            string pass = "reza";
16            ISession session = NHibernateHelper.GetCurrentSession();
17            IQuery query = session.CreateQuery("FROM Account WHERE username = :name  AND pass = :pass ");
18            query.SetString("name", username);
19            query.SetString("pass", pass);
20            IList acc = query.List();
21            session.Close();
22            if (acc.Count == 0)
23                Console.WriteLine("Cannot find specified user");
24            else
25                Console.WriteLine("Found " + acc[0].UserName);
27            Console.ReadKey();
29        }
30    }
It is Work !!!

Thursday, January 12, 2012


1. Installing IIS6 FTP on Windows Server 2003:

If using the new style Start menu: Click on “Start”, “Control Panel”, “Add or Remove Programs” and select the “Add/Remove Windows Components” tab on the left-hand side.

If using the "Classic" style Start menu: Click on “Start”, “Settings”, “Control Panel”, “Add or Remove Programs” and select the “Add/Remove Windows Components” tab on the left-hand side.

In the “Windows Components Wizard”, highlight the “Application Server” and press the “Details” button. The screen below will be displayed (Fig 1).
Figure 1
Figure 1 - The Application Server Screen
Highlight the “Internet Information Services (IIS)” option and press “Details” (as shown in Fig 1 above).
Figure 2
Figure 2 - The Internet Information Services (IIS) Screen
On the next screen (Fig 2 above) we highlight “File Transfer Protocol (FTP) Service”.
Click “OK” to close each window and “Next” to install the newly-selected components. You will be asked to insert your Windows Server 2003 disk. Click “Finish” once the installation is complete.
You have now installed the FTP service.

Figure 3
Figure 3 - Setting up your FTP Root Directory
2. The FTP Root Directory

In order to use FTP in "Isolation" mode, we need to construct the FTP Root so that users are "Dropped" into their correct home directory.

The structure illustrated above contains two subdirectories, "localuser" and my domain "simongibson" which contain home directories for each user. These user sub-directories must match their respective username exactly. If not, the user will not be able to log onto your FTP server.

Create the directory structure above to match your configuration. The "FTPRoot" directory can be placed anywhere on your system.

Figure 4
Figure 4 - Where to find the IIS Manager
3. Where to find the IIS Manager:

If you are using the new style Start menu, you can reach the Internet Information Services console by clicking “Start”, “Administrative Tools” and selecting “Internet Information Services (IIS) Manager” from the list in figure 4 above.
If you are using the “Classic” style Start Menu, you can reach the console by clicking “Start”, “Programs”, “Administrative Tools” and select “Internet Information Services (IIS) Manager” from the list in figure 4 above.

Figure 5
Figure 5 - Removing the Default FTP Site in IIS 6
4. Removing the Default FTP Site in IIS 6:

The first task is to remove (delete) the Default FTP Site. This site does not use Isolation and matches IIS5 FTP sites in terms of functionality and security. As we are going to use Isolation, we will need to create a fresh FTP site.

Simply right-click on the Default FTP Site and press "Delete" in the menu that appears.

Figure 6
Figure 6 - Creating a fresh FTP Site in IIS.
5. Creating a fresh FTP site:

To create a new FTP site, simply right-click on "FTP Sites" and select "New" and "FTP Site...". Then, press "Next" to begin the FTP Site Creation Wizard.

Figure 7
Figure 7 - FTP Site Creation Wizard: FTP Site Description
6. FTP Site Creation Wizard: FTP Site Description

This is the name that will appear in the "FTP Sites" list in IIS. I'm going to use my imagination and call this site "FTP".

Click Next.

Figure 8
Figure 8 - FTP Site Creation Wizard: IP Address and Port Settings
7. FTP Site Creation Wizard: IP Address and Port Settings

Simply select your server's IP address from the list (this is usually the only one listed).

You can also change the TCP Port if required but this is not recommended.

Click Next.

Figure 9
Figure 9 - FTP Site Creation Wizard: FTP User Isolation
8. FTP Site Creation Wizard: FTP User Isolation

This screen allows you to choose the type of Isolation you want to use:-

"Do not isolate users"
Although this option allows users to be "dropped" into their own home directory (if one exists under the FTP root that exactly matches their username), it's NOT able to stop them moving up out of their directory and into those belonging to other users.

"Isolate users"
This option Isolates users based on the directory structure under the FTP root directory (see Step 2). This is the easiest of the two Isolation methods and the method we will use in this tutorial.

"Isolate users using Active Directory"
This option Isolates users by getting their "FTP Home Directory" from the Active Directory. The advantage of this is that new users can be added without the need to modify your FTP site. However, the "FTP Home Directory" can not be entered using the Active Directory snap in and must be configured from the command line by using a VBScript utility.

As shown in Figure 9 above, select "Isolate Users" and press "Next".

Figure 10
Figure 10 - FTP Site Creation Wizard: FTP Site Content Directory
9. FTP Site Creation Wizard: FTP Site Content Directory

This step defines the FTP Root directory. Select the FTP Root directory you created in Step 2 (Figure 3).

Figure 11
Figure 11 - FTP Site Creation Wizard: FTP Site Access Permissions
10. FTP Site Creation Wizard: FTP Site Access Permissions

This step allows you to define read or write access for your FTP site. In this case, I intend to allow files to be uploaded so I've ticked the "Write" box.

Click Next then click Finish to complete the Wizard.

Your FTP Site is now ready for use. To test it, simply open Internet Explorer and enter the URL (or your Server's IP address if different). You should then log in and be automatically "Dropped" into your home directory.