February 18, 2013

Enum: the ‘simple’ C# value type we love to complicate

Filed under: Development — Tags: , , , , , — Arne Joris @ 12:20 pm

Enums are simple collections of constant values, using an underlying integer to store the value.  Enums implement IComparable, IFormattable and IConvertible while still being value types and give us everything we need to store and use things like status values, action codes, colours etc…

You can use enum values in both server and client code, but when the user interface displays an enum, or when the user must be able to choose enums from a combobox, we can run into complications with enums. Making enum display values more human readable and showing values in a particular order are not that easy to implement. In this article, I will show you a solution used in a WPF client consuming WCF web services.

(more…)

January 11, 2013

A .NET-based user interface for MATLAB applications

Filed under: Development — Tags: , , , — Arne Joris @ 6:58 pm

logo_matlab MATLAB is a numerical computing environment for engineering applications. It supports communicating with and controlling hardware and processing and visualizing data from that hardware, which makes it a very popular platform for writing the software that goes with custom hardware solutions.  It also supports creating user interfaces too, but they tend to look outdated and clunky. matlabGUI_example1a

For example, the control and display software for your custom pump controller system can easily be written in MATLAB. It would run on a PC that is connected to your hardware, lets the user turn the system on and off, set parameters such as pump cycle delays and display graphs showing overall efficiency or total energy input. You can compile all the MATLAB code and sell it, along with your hardware and the MATLAB compiler runtime, as a product.

But what are your options when you want a sleeker, more modern UI with a ribbon bar, interactive graphs and UI themes or skins?  The most economic solution with a relatively short time-to-market, is to create a .NET based UI which connects to your MATLAB functions. In this article, I’ll elaborate on how exactly this works.

(more…)

August 13, 2012

Unique number counter system using SQL Server identity fields and Fluent NHibernate

Filed under: Development,NHibernate,SQL Server — Tags: , , — Darrin Maidlow @ 6:00 am

**UPDATE – As we moved forward with our project this method turned out to be a little less awesome than I thought when I put it together.   I’ve left this post here though as I am not ashamed of my failures! There is also some neat stuff in here with setting up the identity fields using nHibernate.

Recently I needed to create a generic re-usable system to generate system wide unique counters using SQL server tables and identity fields to handle the number generation.  One of the key requirements was to be able to specify the identity seed, but in a re-usable way of course.

Generic Class and Class Map

So lets start this off with a base class to define the counter object:

(more…)

November 8, 2011

OracleCommand and parameterized update statements

Filed under: Development,Oracle,RADE — Tags: , , , , — Darrin Maidlow @ 4:07 pm

This week I encountered an irritating situation with Oracle while working on code in the application logic.  In a nutshell I am building dynamically created parameterized insert and update statements based on the RADE metadata and the values entered by the user.  The first call InsertNewRecord works perfectly.  The next call UpdateExistingRecord however was not.  The parameterized SQL was being created.  The parameters were being created and assigned to the .  The ExecuteNonQuery() call was executing without returning an error.  Oracle just would not update.   Even more frustrating – this “just worked” in SQL server.

What were the differences?

The basic logic for insert was this (parts omitted because you probably just don’t care):

  1. Get table metadata
  2. Loop through fields in table
  3. for each field retrieve the value from the UI
  4. Add field to parameterized SQL statement with placeholder
  5. Create new parameter with appropriate name and value.   Add parameter to collection
  6. Loop through parameters in the collection and add to the DbCommand
  7. Finally execute the parameterized SQL statement

As I mentioned this worked great.  Fields were inserted and there was much rejoicing.

The logic for an update was similar but there was one big difference:

  1. Get Table
  2. Loop through fields in table
  3. for each field retrieve the value from the UI
  4. If the field is a key add the placeholder to the where condition, otherwise add the field name and value to the update fields part of the SQL
  5. Create new parameter with appropriate name and value.  Add parameter to collection
  6. Loop through parameters in the collection and add to the DbCommand
  7. Finally execute the parameterized SQL statement.

The branch in step 4 and the if statement ended up causing the problem.

The Problem

defaults to “bind by order” – making the order in which the parameters exist in the SQL statement match the order in which the parameters are added to the OracleCommand. This was happening during the insert because of the structure of an insert statement being so linear. However in the update statement I was building the SQL in a more dynamic way. I was maintaining a list field=value conditions and a separate where condition. In the ended up merging them :

   1: String parameterizedSQL = "UPDATE " + table.Name + " SET " + updateStatement + " WHERE " + whereStatement;

So unless my key field( s) all lined up at the end of the table metadata definitions,  appending that where condition at the end my parameter order got all out of whack in the DbCommand.  So my where condition was actually being set to the wrong value – which could have resulted in the wrong records being updated. Nasty.   Fortunately this can be resolved.

The Fix – BindByName=true

To correct this I had to set the Oracle specific BindByName property to true.  (btw this being the default behavior is just silly.  All the other big data providers default to bind by name and Oracle should too.  That’s a rant for another day though.)   My initial solution was to check if the command is an OracleCommand and if found do a little casting to set the BindByName property then recast it back to DbCommand before executing the query.  Constructive feedback is always welcome!

   1: /// <summary>
   2: /// Execute the parameterized query
   3: /// </summary>
   4: /// <param name="conn">open and active DbConnection</param>
   5: /// <param name="trans">Active DbTransaction</param>
   6: /// <param name="parameterizedSQL">the parameterized SQL</param>
   7: /// <param name="paramList">List of OledDbParameter</param>
   8: /// <returns>DataTable containing the results</returns>
   9: public static void RunParameterizedInsertUpdate(DbConnection conn, DbTransaction trans, String parameterizedSQL, List<DbParameter> paramList)
  10: {
  11:     //create the db command and set the parameterized SQL as a property
  12:     DbCommand command = conn.CreateCommand();
  13:     if(trans != null)
  14:     {
  15:         command.Transaction = trans;
  16:     }
  17:     //hack attack!  By default, Oracle requires its parameters to be placed into the command
  18:     //in the order the parameters appear in the parameterized SQL.  Little hackery here
  19:     //to set the Oracle Command to bind by name
  20:     if (command is Oracle.DataAccess.Client.OracleCommand)
  21:     {
  22:         OracleCommand oraCmd = (OracleCommand) command;
  23:         oraCmd.BindByName = true;
  24:         command = oraCmd;
  25:     }
  26:     command.CommandText = parameterizedSQL;
  27:     command.CommandType = CommandType.Text;
  28:  
  29:     //loop through the params and add them to the command
  30:     foreach (DbParameter parameter in paramList)
  31:     {
  32:         command.Parameters.Add(parameter);
  33:     }
  34:     try
  35:     {
  36:         command.Prepare();
  37:         command.ExecuteNonQuery();
  38:     }
  39:     catch (Exception ex)
  40:     {
  41:         command.Dispose();
  42:         throw;
  43:     }
  44: }

February 2, 2011

Too many languages – they’re melding together into one..

Filed under: Development — Tags: , — Darrin Maidlow @ 12:40 am
   1: (if (== (db_checktrouble) 1)
   2:     (princ "db_Checktrouble returned correct value")
   3:     (alert "db_checktrouble returned wrong value")
   4: )

Writing lisp based unit tests for ObjectARX code written in C# and its all melting together into one….Could this be a new Super Language…..nah…..

Powered by WordPress

Switch to our mobile site