Category Archives: .NET

A short intro on .NET Design by Contract approach

During software development debugging we frequently dig into functions or code libraries and eventually find out that the arguments passed in the first place was mistaken. For example, here is a method using NHibernate repository pattern:

/// <summary>
/// Get the product by the given ID
/// </summary>
/// <param name="productId">The product ID should be a positive integer</param>
/// <returns></returns>
public Product GetProductById(int productId)
    ProductRepository productRepository = new ProductRepository();
    Product product = productRepository.GetById(productId);

    return product;

It happens so often that we fail to notice the comment when calling this method and get a null object from it. We then debug “Object reference not set to an instance of an object” error and figure out the ID should be a positive integer(not that easy to make such an apparent mistake though…). The problem is easy to spot here but not in a more complex method. By applying Design by Contract(DbC) approach the error can be instantly exposed and the method is more robust:
Read the rest of this entry »

Leave a comment

Posted by on August 5, 2012 in .NET, C#, OOP


Accessing Oracle database without installing Oracle Client

A legacy client-server database accessing .NET Windows Form application deployed by ClickOnce. In order to communicate with the database, Oracle Client 8i is installed on every client machine. Clearly installation jobs are disturbing. Another problem is distributed transaction not being supported until Oracle Client 9i. It should be better to implement a WCF service to centralize data access but the application is already too big to reshape.

What I’m trying to achieve here is to enable distributed transaction with newer version Oracle Client without the need to install it on each client PC and still be able to dispatch the application by ClickOnce. It took me a long way to figure this out and make it work. Here are the steps, the files downloaded are for 32-bit application:
Read the rest of this entry »


Posted by on August 1, 2012 in .NET, Database, Oracle


Tags: , ,

Source code generation example using CodeDom

This example demonstrates how to generate source code automatically using .NET framework. It is also intended to serve as a quick reference of common tasks when coding against CodeDom. The example generates a Ford Focus class type in Automobile Namespace and the class inherits Car class. The generated class source code is at the bottom of this article.

// Create a compile unit
CodeCompileUnit compileUnit = new CodeCompileUnit();
// Define a Namespace
CodeNamespace automobileNamespace = new CodeNamespace("Automobile");
// Import Namespaces
automobileNamespace.Imports.Add(new CodeNamespaceImport("System"));


Importing namespaces in this manner puts the using directive in the Automobile namespace scope. Difference
of putting using inside or outside of the namespace is explained at
Should Usings be inside or outside the namespace.
Read the rest of this entry »

Leave a comment

Posted by on July 30, 2012 in .NET, C#, Code Generation


Tags: ,

Query using Oracle Parameter returns no result

I had a parameterized query with some string variables binding to the OracleParameters. The query works with certain parameters but doesn’t with others. Take a simplified database table with only 2 columns for example:


Name Type
Currency Char(3)
SerialNo Char(8)

My original query looks something like this. The first query works. The second runs normally without error message but returns no result when there are records matched in the database.

Dim currency As string = "USD"
Dim serialNo As string = "1311B"

'Quering with cmd1 works
Dim cmd1 As New OracleCommand("SELECT * FROM SampleTable WHERE Currency = :Currency")
cmd1.Parameters.Add(New OracleParameter("Currency", currency))

'Quering with cmd2 works but no record is returned when there are matching records in the database
Dim cmd2 As New OracleCommand("SELECT * FROM SampleTable WHERE SerialNo = :SerialNo")
cmd2.Parameters.Add(New OracleParameter("SerialNo", serialNo))

Read the rest of this entry »

Leave a comment

Posted by on July 23, 2012 in .NET, ADO.NET, Database, Oracle, SQL


NHibernate mappings for Composite Keys with associations

This example demonstrates how to work on mappings with associations for legacy database tables designed purely using multiple primary keys/composite keys. The article NHibernate and Composite Keys by Anne Epstein provides a thorough explanation on fundamental  composite keys mapping and supporting lazy loading by declaring the composite keys as a class object. Here we only focus on a simple code sample of the associations between entities with composite keys.

Considering 2 sample tables, Products and Orders:


Name Type
ProductName VARCHAR


Name Type

Read the rest of this entry »

Leave a comment

Posted by on July 11, 2012 in .NET, C#, NHibernate, ORM


Using Oracle SQL LIKE operator in parameterized OracleCommand

The SQL LIKE operator can be used in the CommandText with Oracle parameters. The syntax of comparing a OracleParameter using SQL LIKE operator is as follows:

Dim cmd As New OracleCommand
cmd.CommandText = "SELECT t.* FROM table t WHERE t.column LIKE :param || '%'"
cmd.CommandText = "SELECT t.* FROM table t WHERE t.column LIKE '%' || :param"
cmd.CommandText = "SELECT t.* FROM table t WHERE t.column LIKE '%' || :param || '%'"

Reference:Usage of Oracle binding variables with LIKE in C#

Leave a comment

Posted by on June 29, 2012 in .NET, ADO.NET, Oracle, SQL, VB


NHibernate & ORA-12571

When I was querying against Oracle database using NHibernate, I ran into Nhibernate.Exceptions.GenericADOException:{“could not update: … The InnerException of it was ORA-12571: TNS:packet writer failure. Resources on the net state that ORA-12571 happens generally when there is a network failure. But in my case it always happens on certain querys. Queries without string type parameters work normally. Turns out it is a Unicode to ASCII conversion problem. Adding a type attribute for string properties in the mapping file as follow can solve it:

<property name="SomePropertyName"  type="AnsiString" ></property>

The exception message is really misleading.


NHibernate and The Case of the Crappy Oracle Error Message

NHibernate and ORA-12571 Errors

Failed to UPDATE or INSERT using NHibernate

Leave a comment

Posted by on June 26, 2012 in .NET, NHibernate, ORM