Thursday, 11 September 2014
Introduction to WPF
Sunday, 31 August 2014
Simple numeric textbox
Private Property DecimalSeparator() As Char
Get
Return m_DecimalSeparator
End Get
Set
m_DecimalSeparator = Value
End Set
End Property
Friday, 29 August 2014
Save image to database using C#
Download project
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace PictureToDatabase
{
public partial class ImageForm : Form
{
private readonly string connectionString = string.Empty;
private OleDbConnection connection;
private OleDbCommand command;
private List
private MyImage currentImage;
///
/// Gets or sets the current image.
///
/// The current image.
///
private MyImage CurrentImage
{
get { return currentImage; }
set
{
currentImage = value;
if (currentImage != null)
{
PictureBox.Image = BinaryToImage(currentImage.FileData);
ImageNameLabel.Text = System.IO.Path.GetFileName(currentImage.FileName);
}
}
}
///
/// Initializes a new instance of the
///
public ImageForm()
{
InitializeComponent();
connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\ImageDatabase.accdb";
}
///
/// Browses the button click.
///
/// The sender.
/// The
private void BrowseButtonClick(object sender, EventArgs e)
{
var browseDialog = new OpenFileDialog
{
Filter = "BMP|*.bmp|GIF|*.gif|JPG|*.jpg;*.jpeg|PNG|*.png|TIFF|*.tif;*.tiff"
};
if (browseDialog.ShowDialog(this) != DialogResult.Cancel)
{
var fileName = browseDialog.FileName;
CurrentImage = new MyImage
{
FileData = System.IO.File.ReadAllBytes(fileName),
FileName = fileName
};
}
}
///
/// Saves the button click.
///
/// The sender.
/// The
private void SaveButtonClick(object sender, EventArgs e)
{
try
{
var filePath = PictureBox.ImageLocation;
var fileData = System.IO.File.ReadAllBytes(filePath);
connection = new OleDbConnection
{
ConnectionString = connectionString
};
connection.Open();
const string statement = @"INSERT INTO Images VALUES(@imageName, @imageData)";
command = new OleDbCommand(statement, connection)
{
CommandType = CommandType.Text
};
command.Parameters.AddWithValue("@imageName", System.IO.Path.GetFileName(filePath));
command.Parameters.AddWithValue("@imageData", fileData);
command.ExecuteNonQuery();
MessageBox.Show("Image saved successfully");
}
catch (Exception exception)
{
MessageBox.Show(string.Format("Error occured {0}", exception.Message));
}
}
///
/// Retrieves the button click.
///
/// The sender.
/// The
private void RetrieveButtonClick(object sender, EventArgs e)
{
connection = new OleDbConnection
{
ConnectionString = connectionString
};
connection.Open();
const string statement = @"SELECT * FROM Images ";
command = new OleDbCommand(statement, connection)
{
CommandType = CommandType.Text
};
var dataTable = new DataTable();
dataTable.Load(command.ExecuteReader());
Images = new List
foreach (DataRow dataRow in dataTable.Rows)
{
Images.Add(new MyImage
{
FileName = dataRow.Field
FileData = dataRow.Field
});
}
if (Images.Count <= 0)
return;
Images.Sort((x, y) => String.CompareOrdinal(x.FileName, y.FileName));
CurrentImage = Images.FirstOrDefault();
ToggleNavigation();
}
///
/// Binaries to image.
///
/// The image data.
///
private Bitmap BinaryToImage(byte[] imageData)
{
Bitmap image;
using (var stream = new System.IO.MemoryStream(imageData))
{
image = new Bitmap(stream);
}
return image;
}
///
/// Nexts the button click.
///
/// The sender.
/// The
private void NextButtonClick(object sender, EventArgs e)
{
var index = Images.IndexOf(CurrentImage);
if (index < Images.Count - 1)
CurrentImage = Images.ElementAt(index + 1);
ToggleNavigation();
}
///
/// Previouses the button click.
///
/// The sender.
/// The
private void PreviousButtonClick(object sender, EventArgs e)
{
var index = Images.IndexOf(CurrentImage);
if (index >= 1)
CurrentImage = Images.ElementAt(index - 1);
ToggleNavigation();
}
///
/// Toggles the navigation.
///
private void ToggleNavigation()
{
NextButton.Enabled = Images.IndexOf(CurrentImage) < Images.Count - 1;
PreviousButton.Enabled = Images.IndexOf(CurrentImage) >= 1;
}
}
///
///
///
public class MyImage
{
///
/// Gets or sets the name of the file.
///
///
/// The name of the file.
///
public string FileName { get; set; }
///
/// Gets or sets the file data.
///
///
/// The file data.
///
public byte[] FileData { get; set; }
}
}
Please leave a comment.
Regex email validator
private static bool IsValidEmailAddress(string email)
{
const string pattern = @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$";
var regex = new System.Text.RegularExpressions.Regex(pattern);
return regex.IsMatch(email);
}
VB
Private Shared Function IsValidEmailAddress(email As String) As Boolean
Const pattern As String = "^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"
Dim regex = New System.Text.RegularExpressions.Regex(pattern)
Return regex.IsMatch(email)
End Function
Thursday, 28 August 2014
Difference between structure and class .NET
- Can support inheritance
- Are reference (pointer) types
- The reference can be null
- Have memory overhead per new instance
- Cannot support inheritance
- Are value types
- Are passed by value (like integers)
- Cannot have a null reference (unless Nullable is used)
- Do not have a memory overhead per new instance - unless 'boxed'
- Are compound data types typically used to contain a few variables that have some logical relationship
- Can contain methods and events
- Can support interfaces
Another example of recursive method.
///
Example of using recursive methods.
{
if (value == 0 || value == 1)
return value;
return (Fibonacci(value - 1) + Fibonacci(value - 2));
}
Wednesday, 4 June 2014
LINQ - Standard Operators - Filtering Operators
ArrayList list = new ArrayList { "Sheraz", new object(), "London", new object() };
var query = from name in list.OfType<string>()
where name == "Sheraz"
select name;
Above query returns only "Sheraz" and narrows down the search to strings only.
Thursday, 26 February 2009
American Standard Code Information Interchange (ASCII)
ASCII, Hexadecimal, Characters and their Names
ASCII | Hex | Char | Name |
0 | 00 | NUL | Null |
1 | 01 | STX | Start of Header |
2 | 02 | SOT | Start of Text |
3 | 03 | ETX | End of Text |
4 | 04 | EOT | End of Transmission |
5 | 05 | ENQ | Enquiry |
6 | 06 | ACK | Acknowledge |
7 | 07 | BEL | Bell |
8 | 08 | BS | BackSpace |
9 | 09 | HT | Horizontal Tabulation |
10 | 0A | LF | Line Feed |
11 | 0B | VT | Vertical Tabulation |
12 | 0C | FF | Form Feed |
13 | 0D | CR | Carriage Return |
14 | 0E | SO | Shift Out |
15 | 0F | SI | Shift In |
16 | 10 | DLE | Data Link Escape |
17 | 11 | DC1 | Device Control 1 (XON) |
18 | 12 | DC2 | Device Control 2 |
19 | 13 | DC3 | Device Control 3 (XOFF) |
20 | 14 | DC4 | Device Control 4 |
21 | 15 | NAK | Negative acknowledge |
22 | 16 | SYN | Synchronous Idle |
23 | 17 | ETB | End of Transmission Block |
24 | 18 | CAN | Cancel |
25 | 19 | EM | End of Medium |
26 | 1A | SUB | Substitute |
27 | 1B | ESC | Escape |
28 | 1C | FS | File Separator |
29 | 1D | GS | Group Separator |
30 | 1E | RS | Record Separator |
31 | 1F | US | Unit Separator |
32 | 20 | [Space] | Space |
33 | 21 | ! | Exclamation mark |
34 | 22 | " | Quotes |
35 | 23 | # | Hash |
36 | 24 | $ | Dollar |
37 | 25 | % | Percent |
38 | 26 | & | Ampersand |
39 | 27 | ' | Apostrophe |
40 | 28 | ( | Open bracket |
41 | 29 | ) | Close bracket |
42 | 2A | * | Asterisk |
43 | 2B | + | Plus |
44 | 2C | , | Comma |
45 | 2D | - | Dash |
46 | 2E | . | Full stop |
47 | 2F | / | Slash |
48 | 30 | 0 | Zero |
49 | 31 | 1 | One |
50 | 32 | 2 | Two |
51 | 33 | 3 | Three |
52 | 34 | 4 | Four |
53 | 35 | 5 | Five |
54 | 36 | 6 | Six |
55 | 37 | 7 | Seven |
56 | 38 | 8 | Eight |
57 | 39 | 9 | Nine |
58 | 3A | : | Colon |
59 | 3B | ; | Semi-colon |
60 | 3C | < | Less than |
61 | 3D | = | Equals |
62 | 3E | > | Greater than |
63 | 3F | ? | Question mark |
64 | 40 | @ | At |
65 | 41 | A | Uppercase A |
66 | 42 | B | Uppercase B |
67 | 43 | C | Uppercase C |
68 | 44 | D | Uppercase D |
69 | 45 | E | Uppercase E |
70 | 46 | F | Uppercase F |
71 | 47 | G | Uppercase G |
72 | 48 | H | Uppercase H |
73 | 49 | I | Uppercase I |
74 | 4A | J | Uppercase J |
75 | 4B | K | Uppercase K |
76 | 4C | L | Uppercase L |
77 | 4D | M | Uppercase M |
78 | 4E | N | Uppercase N |
79 | 4F | O | Uppercase O |
80 | 50 | P | Uppercase P |
81 | 51 | Q | Uppercase Q |
82 | 52 | R | Uppercase R |
83 | 53 | S | Uppercase S |
84 | 54 | T | Uppercase T |
85 | 55 | U | Uppercase U |
86 | 56 | V | Uppercase V |
87 | 57 | W | Uppercase W |
88 | 58 | X | Uppercase X |
89 | 59 | Y | Uppercase Y |
90 | 5A | Z | Uppercase Z |
91 | 5B | [ | Open square bracket |
92 | 5C | \ | Backslash |
93 | 5D | ] | Close square bracket |
94 | 5E | ^ | Caret / hat |
95 | 5F | _ | Underscore |
96 | 60 | ` | Grave accent |
97 | 61 | a | Lowercase a |
98 | 62 | b | Lowercase b |
99 | 63 | c | Lowercase c |
100 | 64 | d | Lowercase d |
101 | 65 | e | Lowercase e |
102 | 66 | f | Lowercase f |
103 | 67 | g | Lowercase g |
104 | 68 | h | Lowercase h |
105 | 69 | i | Lowercase i |
106 | 6A | j | Lowercase j |
107 | 6B | k | Lowercase k |
108 | 6C | l | Lowercase l |
109 | 6D | m | Lowercase m |
110 | 6E | n | Lowercase n |
111 | 6F | o | Lowercase o |
112 | 70 | p | Lowercase p |
113 | 71 | q | Lowercase q |
114 | 72 | r | Lowercase r |
115 | 73 | s | Lowercase s |
116 | 74 | t | Lowercase t |
117 | 75 | u | Lowercase u |
118 | 76 | v | Lowercase v |
119 | 77 | w | Lowercase w |
120 | 78 | x | Lowercase x |
121 | 79 | y | Lowercase y |
122 | 7A | z | Lowercase z |
123 | 7B | { | Open brace |
124 | 7C | | | Pipe |
125 | 7D | } | Close brace |
126 | 7E | ~ | Tilde |
127 | 7F | DEL | Delete |



