Logging Experiences

T-SQL: remove or replace line breaks

Posted in Programming, SQL, Tips and Tricks by Sina Iravanian on September 6, 2012

Sometimes you might need to remove line breaks from a column value to make a one-line data. I prefer to replace line-breaks with a single space, rather than removing them. This is how to do it with Transact-SQL:

REPLACE(REPLACE(ISNULL( SomeTable.SomeColumn, ''), CHAR(13), ''), CHAR(10), ' ')

To remove line-breaks completely replace the final space string with an empty string.

Tagged with: ,

T-SQL: checking if a string is empty or white-space

Posted in Programming, SQL, Tips and Tricks by Sina Iravanian on September 6, 2012

Consider a case where you want to bring through a varchar column called SomeColumn from table SomeTable and replace its value with, 'Not Available' if the value is NULL or an empty string. This is how it can be achieved in Transact-SQL:

SELECT ISNULL(NULLIF(SomeTable.SomeColumn, ''), 'Not Available')
FROM SomeTable

The above code is checking for null or emptiness of the string. To check for being null or white-space, use the following code instead:

SELECT ISNULL(NULLIF(LTRIM(RTRIM(SomeTable.SomeColumn)), ''), 'Not Available')
FROM SomeTable
Tagged with: , ,

An example for C# dynamic in action

Posted in C#, Programming, Tips and Tricks by Sina Iravanian on July 23, 2012

Recently I was involved with enabling validation for a Windows Forms form containing several pages shown through a tree view. There were one user control defined for each page, therefore whenever a node in tree-view got activated, an instance of that user-control were shown. The user-controls did not have a common user-defined parent base-class or interface. The author of the existing code had defined a Save method in each control separately which performed updating data with values entered in the forms.

First, I changed all void Save() methods to bool Save(), and performed the validation stuff in each control separately. Then in the parent form, I created an array of Controls, so that I could loop through them, and call their Save method. But wait, Save is not inherited from any base-class or interface, so there’s no polymorphism at hand. How can I call them through references of type Control?

Well honestly, the best solution would be adding a new interface to the project, say ISavableControl or something, add the Save method to it, and let the controls implement that interface. But I wanted to make a quick fix to the problem without adding new files and types to the code. Here C#’s dynamic comes into action.

Using dynamic keyword, one postpones binding methods, properties, and fields to run-time. In our scenario we know that the user-controls each have a method called Save, which is not derived from a parent. Without dynamic, I would need to have a reference explicitly from each user control’s type, and I would have to call them one by one, as in:

bool succeeded = false;
succeeded = userControl1.Save();
succeeded = succeeded || userControl2.Save();
succeeded = succeeded || userControl3.Save();
// ...
succeeded = succeeded || userControln.Save();

However using dynamic keyword, life gets a lot easier:

// objects are not inherited from a base class or interface,
// therefore we cannot call Save using polymorphism
bool succeeded = false;
foreach(var ctrl in controlsArray)
    dynamic dynCtrl = ctrl;
    succeeded = succeeded || dynCtrl.Save();

// do something with the value of succeeded

Take care that binding the Save methods to controls happen at runtime. Therefore if a control does not implement a Save method, or if another one misspells Save, as say Svae, you will get no hints at compile time. So, try to avoid this style of coding, unless you have good reasons for doing so.

Tagged with: , ,

How I built my first Qt Application in Visual Studio 2010

Posted in C++, Qt, Tips and Tricks by Sina Iravanian on July 4, 2011

I downloaded the Qt SDK (offline installer version which was 1.7 GB) from:

At the time of this writing, the Qt libraries version was 4.7.3.

Then I downloaded the VS-Addin, using the link at the bottom of the downloads page, titled "Qt Visual Studio Add-in".

After installing both SDK, and the VS-Addin on my system, I started Visual Studio 2010. I tried to create a sample project by selecting: File > New > Project… > Qt4 Projects > Qt Application, and specified proper values for Name and Location. After going through the Qt wizard to the end, I encountered the following error message:

unable to find Qt Build!
To solve this problem specify Qt Build!

By going to the Qt setup directory, one sees that there are Qt SDKs available for different target platforms. For example I installed Qt in "D:\QtSDK\" and in that directory I see some folders which correspond to different target platforms such as Desktop, Madde, Symbian, and others. An appropriate platform directory should contain a bin folder inside. This is true about Madde, and Symbian, but the Desktop folder is a root for subdirectories related to different Qt versions and C++ compilers. Since I wanted to develop Qt applications with Microsoft Visual C++ for the Windows Desktop platform I went to the following directory which contains the required bin folder:

For the next step, I had to introduce this directory for the Qt VS-addin. From the main menu, I selected Qt > Qt Options > Qt Versions. I pressed the Add button, and entered "Win 4.7.3" in the  "Version name" field, and entered the above path in the "Path" field, and pressed OK. The path that I specified was added to the gird. Then I made sure that in the Default Qt/Win version, the version that I have just specified (i.e., Win 4.7.3) is selected. I pressed OK, and tried once more with the Qt New Project wizard. This time everything went OK, and I succeeded to create and build my first Qt Application in the VS 2010 environment.

Note: In fact the current Qt binaries are targeted for Visual Studio 2008. I was able to use the binaries (i.e., libs and dlls) because I had also Visual Studio 2008 and the corresponding C++ compiler installed on my machine. Otherwise, I presume that I would need to recompile the sources with the VS-2010 C++ compiler.

Tagged with: ,

How to disable hibernate and free some disk space in Windows 7

Posted in Tips and Tricks by Sina Iravanian on May 26, 2011

Run a command prompt as administrator.

Run the following command:

powercfg.exe -h off

The disk space is freed immediately. To turn it back on run:

powercfg.exe -h on

Reference: +

Including .eps images with pdflatex

Posted in LaTeX, Tips and Tricks by Sina Iravanian on May 21, 2011

PdfLaTeX does not support eps files by default. Add the following imports in the beginning:

\usepackage{graphicx} % already added

After that the includegraphics commands with eps arguments should produce no problems.

Reference: +

Tagged with: ,

Problem in finding local resources in Intellij IDEA

Posted in Java, Programming, Tips and Tricks by Sina Iravanian on May 10, 2011

Some classes use external data files which are located in the same directory as the one that the .java file is located, or somewhere nearby which can be easily addressed with a relative path. This happens a lot for me, esp. when I am running open source programs developed by someone else. As an example, imagine a class called SampleClass located in a directory in which there exists a text file with the name of “SomeFile.txt”. One way to have access to the file is through calling the class loader’s getResource function:

URL theURL = this.getClass().getResource("SomeFile.txt");

I tested this piece of code in NetBeans and it works fine. But not in Intellij IDEA! To solve this (annoying) problem, I needed to add the path of the source directory to classpath. For this purpose I did the following steps in Intellij IDEA 10.0.3 Community Edition:

  1. Right click on the module name and select "Open Module Settings"
  2. Select "Modules" from the leftmost pane
  3. Select "Dependencies" tab
  4. Press "Add…" button and select "1 Single-Entry Module Library…" from the popup menu
  5. Find and select the "src" folder of your sources.
  6. Under the "Scope" column, change the scope to "Runtime"
  7. Press Apply and OK

Dealing with ListView control, in the details mode

Posted in C#, Programming, Snippets, Tips and Tricks by Sina Iravanian on May 9, 2011

I needed a piece of code to manage list-view controllers. I needed to use ListView controls in the details mode, which automatically sort its contents when I click on the header, and also pastes the content in a tabular format when I press Ctrl+C or Ctrl+Insert on it. So I created the following classes. It is necessary to note that the code that carries out the sorting stuff when the header is clicked is taken from the following MSDN article:

So these are the contributions of these pieces of code: 1) Sorting ListView items automatically when the header is clicked, 2) Pasting the contents of the ListView control to the clipboard in a tabular format, and 3) Providing an easy to use API to append data to a ListView control.

The following gist contains the code to accomplish this. It consists of 3 files. The ListViewColumnSorter.cs file is responsible for the sorting stuff. The ListViewUtils.cs file provides the utility methods for a ListView control. The main methods that perform the job are located in the ListViewUtils class. The two needed methods are InitListView and AppendToListView. And finally the Usage.cs file provides a small sample demo of the utility methods.

Enabling Middle Click in Synaptics Touchpad Drivers

Posted in Tips and Tricks by Sina Iravanian on April 16, 2011

Recently I upgraded my currently installed Synaptics touchpad driver to version 15.x, and I found that I cannot emulate the middle-click effect by pressing both right and left click buttons. I found no options in the settings to enable that functionality. Finally after searching the web I found a workaround here [+]:

  1. Run the registry editor (regedit)
  2. Go to key: HKEY_LOCAL_MACHINE\Software\Synaptics\SynTP\Defaults\
  3. Here create a new key as a DWORD and name it as 'HasBothButtonFeature' without the quotes and set the value to 1.
  4. Log off and Log on again

It worked fine for me.

Tips and Tricks: Making Windows Forms Size to Their Content

Posted in C#, Programming, Tips and Tricks by Sina Iravanian on August 15, 2010

Windows In WPF can make use of a property which makes the window to automatically adjust its own size to fit its content. The related property in WPF is SizeToContent which can be set to Manual (the default), Width, Height and WidthAndHeight. The last item makes the window shrink or grow so that it will fit its content.

The same effect can be made in Windows Forms using the AutoSize and AutoSizeMode properties of the form. Simply set AutoSize to true and also do not forget to set AutoSizeMode to GrowAndShrink. This effect is not visualized during design.

Tagged with: ,