Posts

Showing posts from August, 2014

Await in Catch and Finally

This is just a brief note to publicize a coming improvement to the async language support. With the new compilers, changes to the C# language (e.g., async/await) are easier than they used to be. One improvement that is coming is the use of await in catch and finally blocks. This enables your error-handling/cleanup code to be asynchronous without awkward code mangling. For example, let’s say that you want to (asynchronously) log an exception in one of your async methods. The natural way to write this is: try {   await OperationThatMayThrowAsync(); } catch (Exception ex) {   await MyLogger.LogAsync(ex); } And this natural code works fine in Visual Studio “14”. However, the currently-released Visual Studio 2013 does not support await in a catch, so you would have to keep some kind of “error flag” and move the actual error handling logic outside the catch block: Exception exception = null; try {   await OperationThatMayThrowAsync(); } catch (Exception ex) {   exception...

HEX to ASCII and ASCII to HEX Class - C#

This article shows you how to convert string to hexadecimal and vice versa. HEX TO ASCII Class using System.Collections.Generic; using System.Text; using Microsoft.VisualBasic; // I'm using  this class for Hex Converion namespace Hex_Converter {     public class HexConverter     {         public string Data_Hex_Asc(ref string Data)         {             string Data1 = "";             string sData = "";             while (Data.Length > 0)             //first take two hex value using substring.             //then  convert Hex value into ascii.             //then convert ascii value into character.             {                 Data1 = System.Convert.ToChar(System.Convert...

Message Box Class - Confirm,Error, Warning and Info - C#

A C# Snippet for creating Message Box with various type of message (Confirm,Error, Warning and Info). Message Box Class using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; namespace messageBox {     public static class MsgBox     {         // Displays a Confirm type message box             // parameter name="sMsg" is The message you want to display         public static bool Confirm(string sMsg)         {             return Confirm("Confirm :", sMsg);         }         public static bool Confirm(string sTitle, string sMsg)         {             DialogResult ret = MessageBox.Show(sMsg, sTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question);             return (ret == DialogResult.Yes); ...

Convert Hex / Css String color to .NET Colour (RGB)

Image
Have you facing problem to convert "#CCCCCC" to .NET Colour.? Here is a solution to convert the CSS / Hex String to .NET Colour (RGB). Conversion Class using System; using System.Collections.Generic; using System.Text; using System; using System.Drawing; using System.Text.RegularExpressions; namespace Convert_Hex_String_to.NET_Color {     public class ConversionClass     {               /// <summary>         /// Convert a hex string to a .NET Color object.         /// </summary>         /// <param name="hexColor">a hex string: "FFFFFF", "#000000"</param>         public static Color HexStringToColor(string hexColor)         {             string hc = ExtractHexDigits(hexColor);             if (hc.Length != 6)             { ...

Register winform app HotKeys - C#

Image
Hotkey is a very useful shortcut for complex application. Sometimes we build application with much feature include in it hence will result of bad experience of end user because the feature dont have the shortcut keys. Here is a sample to add shortcut keys or hotkeys for winform app in C# code. GlobalHotkeys.cs using System; using System.Windows.Forms; using System.Runtime.InteropServices; namespace Hotkeys {     public class GlobalHotkey     {         private int modifier;         private int key;         private IntPtr hWnd;         private int id;         public GlobalHotkey(int modifier, Keys key, Form form)         {             this.modifier = modifier;             this.key = (int)key;             this.hWnd = form.Handle;         ...

Get each table size in mssql database

This is the snippet to get each table size in the schema/catalogue database. MSSQL Code SELECT     t.NAME AS TableName,     s.Name AS SchemaName,     p.rows AS RowCounts,     SUM(a.total_pages) * 8 AS TotalSpaceKB,     SUM(a.used_pages) * 8 AS UsedSpaceKB,     (SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB FROM     sys.tables t INNER JOIN         sys.indexes i ON t.OBJECT_ID = i.object_id INNER JOIN     sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id INNER JOIN     sys.allocation_units a ON p.partition_id = a.container_id LEFT OUTER JOIN     sys.schemas s ON t.schema_id = s.schema_id WHERE     t.NAME NOT LIKE 'dt%'     AND t.is_ms_shipped = 0     AND i.OBJECT_ID > 255 GROUP BY     t.Name, s.Name, p.Rows ORDER BY     t.Name By Mohd Zulkamal NOTE : – If You have Found this pos...