24 February 2011

HTML - Trigger button on enter key

The following will cause a buttons click event to fire when pressing [Enter] in a text box:
<input type="text" onkeypress="if(event.keyCode==13) document.getElementById('btnToTrigger').click()" />

23 February 2011

.NET - String to hexadecimal and vice versa

The following functions encode a string to hexadecimal form and the other way round. I use this method to avoid encoding issues in data files:
protected string EncodeStrToHex(string str) {
  if (string.IsNullOrEmpty(str))
    return str;
  byte[] chars = Encoding.UTF8.GetBytes(str);
  StringBuilder result = new StringBuilder(chars.Length);
  foreach (byte c in chars) {
    result.Append(c.ToString("X2"));
  }
  return result.ToString();
}

protected string EncodeHexToStr(string hex) {
  if (string.IsNullOrEmpty(hex))
    return hex;
  byte[] chars = new byte[hex.Length / 2];
  string c = string.Empty;
  for (int i = 0, j = 0; i < chars.Length; i++, j += 2) {
    c = new string(new char[] { hex[j], hex[j + 1] });
    chars[i] = byte.Parse(c, NumberStyles.HexNumber);
  }
  return Encoding.UTF8.GetString(chars);
}

ASP.NET - ValidateRequest and .NET Framework 4

When building a website in ASP.NET using the .NET Framework 4, ValidateRequest doesn't work as it used to anymore.

The solution lies in the Web.config, where you add:
<httpRuntime requestValidationMode="2.0" />
to the
<system.web>
section.

08 February 2011

.NET - Generic casting to nullable types

The following is part of a function that reads a value from an XML node and casts it to a given type. The function uses:

Convert.ChangeType(value, type)

to do the casting, but this doesn't work for nullable types.
To solve this problem, the following section was added:

else if (type.IsGenericType &&
         type.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) {
  if (string.IsNullOrEmpty(value)) {
    return defaultValue; //Commonly 'null'
  }
  else {
    NullableConverter nullConv = new NullableConverter(type);
    return Convert.ChangeType(value, nullConv.UnderlyingType);
  }
}

The result of this function, which returns an object, is casted to the desired - nullable - type on the line where it is called.

02 February 2011

CRM - Register and use a plugin

To register and use a plugin in CRM (2011 online), you best use the Plugin Registration Tool provided in the CRM SDK:
  • I not yet: download and install Windows Identity Foundation,
  • Build the Plugin Registration Tool source code.

Register the plugin via the tool:
  • Add a connection to the CRM server,
  • Register > Register New Assembly, and select which plugins,
  • Choose 'Sandbox' and 'Database', and click 'Register'.

To use the plugin via the tool:
  • Register > Register New Step
  • 'Message': the event on which to run,
  • 'Primary Entity': the entity on which that event occurs,
  • Select when to run relative to the event itself,
  • Click 'Register New Step'.

01 February 2011

.NET/SQL - Split multiple statements

For the project 'DatabaseTool', I had to split a query that could contain multiple statements into those multiple statements. For this I required each statement to end with a ';' and start on a new line. I wrote the following function to split them:

public IEnumerable GetStatements(string query) {
  string[] parts = query.Split(new string[] { ";\r\n" },
                               StringSplitOptions.RemoveEmptyEntries);
  return from p in parts
         select p.Trim(new char[] { ' ', ';', '\t' });
}

I didn't split on white lines because those can also occur within statements; and it's to risky when UPDATE's and WHERE's are involved.