Thursday, December 3, 2015

SqlParameter does not accept 'null' value

In .NET, if you want to call DbContext.Database.ExecuteSqlCommand and use SqlParameter to pass parameters to the SQL command, and if one of your parameters contains value 'null', you cannot simply pass the 'null' as the value of the parameter. Instead, you need to pass DBNull.Value as a null parameter within SQLCommand.

Also, you cannot use ?: operator to do something like this:

(Assume yourParameter is the variable that contains some value, and yourSqlParameter is the variable you want to pass into SqlCommand as the parameter.

SqlParameter yourSqlParameter = new SqlParameter("@FieldName", SqlDbType.CertainDbType);
yourSqlParameter.Value = (yourParameter == null) ? DBNull.Value : yourParameter;

because yourParameter type is not compatible with DBNull.Value. In the MSDN documentation for the ?: operator states the following:

Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other.


Therefore, we need to do something like:


if (yourParameter == null) 
{
    yourSqlParameter.Value = DBNull.Value;
}
else
{
    yourSqlParameter.Value = yourParameter;
}

Wednesday, December 2, 2015

How to show ellipsis if the button label (text inside the button) is too long

Add the following style to the button:

.btn-long-label {
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
}

Thursday, October 2, 2014

Using WPF user control in Winform application brings issue on a High-DPI display setting, and solution

Problem

One of our Winform applications uses a WPF user control, which is hosted in a System.Windows.Forms.Integration.ElementHost control. It worked fine until recently we tried to run it on a new 10-inch tablet which has Windows 8.1 as the OS. The font of the app became very tiny on the screen and almost unreadable. The resolution of the tablet was 1920 * 1200 and the DPI was set to 'Large', which was 144(150%), which was the default Windows 8.1 setting on this tablet. Our other Winform applications which did not using WPF control were scaled automatically to the normal, human-readable size. Only this one with WPF control had the problem.

I searched on Google about ElementHost scale problem something like that and found that some people using ElementHost or WPF control in Winform application encountered similar problem, but no one provided a valid solution.

Solution

After a couple of hours research, I finally found a very simple solution -- adding <dpiAware> to the application manifest. Here is an example from Microsoft's website.

<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3" >
  <asmv3:application>
    <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
      <dpiAware>True</dpiAware>
    </asmv3:windowsSettings>
  </asmv3:application>
</assembly>

For my case, I need to set the <dpiAware> to  Per-monitor to make it work normally. That is, change the line in the middle to <dpiAware>Per-monitor</dpiAware>. The differences between each value are listed below(These are from MSDN, for more detail information, please see the reference links below).
DPI awareness manifest valueDescription
FalseSets the application to not DPI-aware.
TrueSets the application to system DPI–aware.
Per-monitorOn Windows 8.1, sets the application to per monitor-DPI aware. On Windows Vista through Windows 8, sets the application to not DPI–aware.
True/PMOn Windows 8.1, sets the application to per monitor-DPI aware. On Windows Vista through Windows 8, sets the application to system-DPI aware.

So that is something about DPI-Aware application. For those who is interested in how to write a DPI-Aware application, there are some very detailed contents on Microsoft's website:

Tutorial: Writing High-DPI Win32 Applications

Writing DPI-Aware Desktop and Win32 Applications

Developing a Per-Monitor DPI-Aware WPF Application

Friday, January 3, 2014

A few guidelines on WPF GridSplitter Control, from Matthew MacDonald's book Pro WPF in C# 2010

Most programmers find that the GridSplitter isn’t the most intuitive part of WPF. Understanding how to use it to get the effect you want takes a little experimentation. Here are a few guidelines:

The GridSplitter must be placed in a Grid cell. You can place the GridSplitter in a cell with existing content, in which case you need to adjust the margin settings so it doesn’t overlap. A better approach is to reserve a dedicated column or row for the GridSplitter, with a Height or Width value of Auto.

The GridSplitter always resizes entire rows or columns (not single cells). To make the appearance of the GridSplitter consistent with this behavior, you should stretch the GridSplitter across an entire row or column, rather than limit it to a single cell. To accomplish this, you use the RowSpan or ColumnSpan properties you considered earlier. For example, the GridSplitter in Figure 3-15 has a RowSpan of 2. As a result, it stretches over the entire column. If you didn’t add this setting, it would appear only in the top row (where it’s placed), even thoughdragging the splitter bar would resize the entire column.

Initially, the GridSplitter is invisibly small. To make it usable, you need to give it a minimum size. In the case of a vertical splitter bar (like the one in Figure 3-15), you need to set VerticalAlignment to Stretch (so it fills the whole height of the available area) and Width to a fixed size (such as 10 device-independent units). In the case of a horizontal splitter bar, you need to set HorizontalAlignment to Stretch and set Height to a fixed size.

The GridSplitter alignment also determines whether the splitter bar is horizontal (used to resize rows) or vertical (used to resize columns). In the case of a horizontal splitter bar, you should set VerticalAlignment to Center (which is the default value) to indicate that dragging the splitter resizes the rows that are above and below. In the case of a vertical splitter bar (like the one in Figure 3-15), you should set HorizontalAlignment to Center to resize the columns on either side.

from Matthew MacDonald's book Pro WPF in C# 2010

Friday, October 11, 2013

WebBrowser DocumentCompleted event fired more than once

How To Determine When a Page Is Done Loading in WebBrowser Control DocumentCompleted is WinForms' wrapper of the DocumentComplete evert, however WebBrowserDocumentCompletedEventArgs hides the sender parameter so you cannot tell which frame is raising the event. Alternatively you can check WebBrowser.ReadyState.

By Sheng Jiang 蒋晟

Wednesday, October 9, 2013

Use NumberStyles parameter in Parse and TryParse

When you want to parse string to int which contains various style or format, such as currency symbol, thousands separator, etc., use TryParse(String, NumberStyles, IFormatProvider, Int32). See more:

Int32.TryParse Method (String, NumberStyles, IFormatProvider, Int32)

http://msdn.microsoft.com/en-us/library/zf50za27.aspx