Saturday, 14 February 2015

Download ALL Old Versions of iTunes


Found a really great website that let's you download ALL older versions of iTunes for iOS, Windows 32bit and Windows 64bit!

Website: http://appstudio.org/itunes/

Wednesday, 11 February 2015

C# - Serialize and Deserialize using BinaryFormatter with Strings (WITHOUT Files)


This code snippet will allow you to Serialize and DeSerialize any serializable object to a standard string and back. No need for binary files!

Usage:
Code Snippet
  1. // Inits
  2. CustomClass instance = new CustomClass();
  3.  
  4. // Serialize
  5. string data = SerializeObject<CustomClass>(instance);
  6.  
  7. // DeSerialize
  8. CustomClass instance = DeSerializeObject<CustomClass>(data);
End of Code Snippet


Code Snippet
  1. // NOTE: The result of serializing an object with BinaryFormatter is an octet stream, not a string.
  2. //       If we encode the serialized object with Base64, we will get our string.
  3. public static string SerializeObject<T>(T toSerialize)
  4. {
  5.     System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  6.  
  7.     if (!toSerialize.GetType().IsSerializable)
  8.     {
  9.         throw new Exception("Object cannot be serialized in it's current state.");
  10.     }
  11.  
  12.     using (MemoryStream ms = new MemoryStream())
  13.     {
  14.         new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter().Serialize(ms, toSerialize);
  15.         return Convert.ToBase64String(ms.ToArray());
  16.     }
  17. }
  18.  
  19. public static T DeSerializeObject<T>(string data)
  20. {
  21.     byte[] bytes = Convert.FromBase64String(data);
  22.  
  23.     using (MemoryStream ms = new MemoryStream(bytes))
  24.     {
  25.         return (T)new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter().Deserialize(ms);
  26.     }
  27. }
End of Code Snippet

Friday, 6 February 2015

Recursion and Recursive Methods for Dummies - Working C# Example


In Brief
What is Recursion: Very simply put... A method that calls itself.
Why use Recursion?: So we can repeat a methods logic any number of times with differing arguments.
What is an Example of Recursion?: Directories which contain directories which contain more directories and you would like to scan through all of the files within this directory chain. We can create ONE SINGLE method to do this by calling itself recursively. See below for full example.

Here is a working example of a recursive method in C#. It takes a file directory as an argument and recursively loops through all of the files which the WHOLE directory structure. It goes in deep, then works backwards. This is an example of backwards or tail recursion.

Code Snippet
  1. public void NameOfRecursiveMethod(string directoryToSearch)
  2. {
  3.     // Go in deep as far as possible, then work backwards.
  4.     // Find all the subdirectories under this directory.
  5.     foreach (string dir in Directory.GetDirectories(directoryToSearch))
  6.     {
  7.         // Recursive call for each subdirectory.
  8.         this.NameOfRecursiveMethod(dir);
  9.     }
  10.    
  11.     // Note: The First time we arrive here will be inside the deepest first directory
  12.     // Get all files in the directory
  13.     string[] files = Directory.GetFiles(directoryToSearch);
  14.  
  15.     foreach (string filename in files)
  16.     {
  17.         // Do something for each file here
  18.        
  19.     }
  20. }
End of Code Snippet

How to Deep Clone an Object by Value and not by Reference in C#


The following code snippet using serialization to deep clone an object in C#. Please note: ICloneable is a deprecated API. ICloneable is considered a poor API, since it does not specify whether the result is a deep or a shallow copy.

Code Snippet
  1. // Usage
  2. CLASSNAME clonedInstance = DeepClone<CLASSNAME>(instance);
  3.  
  4.  
  5. // Deep clones an object
  6. // Your class (and all sub-classes) MUST be marked as [Serializable] in order for this to work.
  7. public static T DeepClone<T>(T obj)
  8. {
  9.     using (var ms = new System.IO.MemoryStream())
  10.     {
  11.         var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  12.         formatter.Serialize(ms, obj);
  13.         ms.Position = 0;
  14.  
  15.         return (T)formatter.Deserialize(ms);
  16.     }
  17. }
End of Code Snippet

Sunday, 18 January 2015

SQL - Find Duplicate Data Column Values


Here is a quick snippet on how to find duplicate columns within your tables

Code Snippet
  1. select col1, col2, count(*)
  2. from TABLE
  3. group by col1, col2
  4. having count(*) > 1
End of Code Snippet

Tuesday, 13 January 2015

Free DNS Tools: Ping, Lookup, Trace, Whois, DNS Records, Spam Blacklist Check, Network Lookup and more!


http://network-tools.com/ is a great website that allows you to perform an array of network and troubleshooting steps on any domain or IP address. I use it a lot when troubleshooting domain nameservers and other things that domain registrars should get right, but fail to do so!

Some of the sample operations that can be performed on a domain:
Express
Ping
Lookup
Trace
Whois (IDN Conversion Tool)
DNS Records (Advanced Tool)
Network Lookup
Spam Blacklist Check
URL Decode
URL Encode
HTTP Headers with optional SSL
Email Tests

Check it out!

Sunday, 11 January 2015

Binding a DataGridView to a Collection - Notify Grid When Properties are Changed (BindingList and INotifyPropertyChanged)


When utilising a DataGridView or similar control within your windows and web applications, you may wish to bind to a collection (List, ArrayList etc). Binding is always great as you can maintain and manage your custom objects very easily. What can be tough to manage is when you may wish to edit the controls data (A cells contents within a DataGridView for example), so you have to edit the data within your data source (I.e. List of custom objects), then re-bind to reflect the new changes.

The method above would work, but utilising a more Observer Pattern approach to our programming, we can automatically notify the grid when we update any of our properties within our custom collection. You simply need to inherit from INotifyPropertyChanged within your custom object and bind to a DataGridView with a BindingList collection (Very similar to a List). Within the set properties for your custom object, simply raise an event.

A full example can be found here: http://tech.pro/tutorial/776/csharp-tutorial-binding-a-datagridview-to-a-collection

Free DataGrid / DataGridView for WinForms and ASP.NET MVC (SourceGrid)


SourceGrid is a .NET Windows Forms grid control written entirely in C# with managed code. SourceGrid can be used to visualize or to change data in a table format.



Features
- three sub-components, Grid, DataGrid, ArrayGrid
- Multi selection mode, Row Selection, Column selection, Free selection
- Cell and Row spanning
- MVC support
- Full feature list

Website: http://sourcegrid.codeplex.com
Code Project: http://www.codeproject.com/Articles/3531/SourceGrid-Open-Source-C-Grid-Control

Tuesday, 16 December 2014

Lock Web.Config ASP.NET Configuration Settings


By default, ASP.NET configuration files that are located in subdirectories override and extend configuration settings that are declared in parent configuration files. In application hosting scenarios, you might want to lock some settings of an ASP.NET application to prevent modification at lower levels. For example, you can lock the security settings for hosted applications to help prevent administrators from inadvertently changing those security settings.

You can lock configuration settings in ASP.NET configuration files (Web.config files) by adding an allowOverride attribute to a location element and setting the allowOverride attribute to false. Then within the location element, you can define the configuration section that you want to lock. ASP.NET will throw an exception if another configuration file attempts to override any configuration section that is defined within this locked location element.

Using a location element with an allowOverride=false attribute locks the entire configuration section. You can also lock individual configuration elements and attributes using lockItem, lockElements, lockAttributes, lockAllAttributesExcept, and lockAllElementsExcept.

Example:
The following code example shows part of a Web.config file that locks the trust level of two different ASP.NET applications: application1 and application2. Any attempt to override the configuration settings in the trust configuration section raises a configuration system error.
Code Snippet
  1. <configuration>
  2.   <location path="application1" allowOverride="false">
  3.     <system.web>
  4.       <trust level="High" />
  5.     </system.web>
  6.   </location>
  7.  
  8.   <location path="application2" allowOverride="false">
  9.     <system.web>
  10.       <trust level="Medium" />
  11.     </system.web>
  12.   </location>
  13. </configuration>
End of Code Snippet

Avoid Web.Config Inheritance in Child Web Application by using location inheritInChildApplications = false


With shared hosting platforms like: HostGator, GoDaddy, Dreamhost to name a few, you can easily pickup some entry level Windows Plesk Hosting with all the ASP.NET trimmings quite cheaply! If you're an avid developer with a hand in all the pies, you probably have at least 5 or more web apps and/or web services you want to host all in the same shared hosting space!
If this IS your scenario, beware that entry level shared hosting plans have ONE APP POOL for ALL of your apps! See my post on Application Pools and Web Applications for more information.

Why may I need to do this?
If you have a web application, for example, sitting in your root directory and another web application/service sitting in a sub folder, then by default, this child web app will inherit the Web.Config settings of the root application.
If your root application is .NET 4.5 and your sub-apps are .NET 2.0, then you may already be seeing a few fancy errors! This is just one of many reasons why we need to prevent this. This post outlines TWO methods to do this...

Method 1
You can prevent inheritance by utilizing the location tag's inheritInChildApplications attribute. You should put the location tag in the ROOT Web.Config and NOT the child app's Web.Config in our example. The path attribute of the location tag is set to a wildcard (.) to specify ANY child app location. You can set this to a path attribute to target a single app if you prefer by entering it's location. As good practice, you should wrap individual tags with the location tag (The location tag can appear more than once in the Web.Config file!). Do not wrap EVERYTHING in the Web.Config with the location tag.
Code Snippet
  1. <?xml version="1.0"?>
  2. <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
  3. <!-- disable inheritance for the connectionStrings section -->
  4. <location path="." inheritInChildApplications="false">
  5.    <connectionStrings>
  6.    </connectionStrings>
  7. </location>
  8.  
  9. <!-- leave inheritance enabled for appSettings -->
  10. <appSettings>
  11. </appSettings>
  12.  
  13. <!-- disable inheritance for the system.web section -->
  14. <location path="." inheritInChildApplications="false">
  15.    <system.web>
  16.         <webParts>
  17.         </webParts>
  18.         <membership>
  19.         </membership>
  20.  
  21.         <compilation>
  22.         </compilation>
  23.       </system.web>
  24.  </location>
  25. </configuration>
End of Code Snippet

Note: I included xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0" as an attribute of the configuration tag as inheritInChildApplications is defined in the .NET 2.0 schema. Setting this is good practise and will also remove the warning in Visual Studio about the inheritInChildApplications attribute not being recognized.
WARNING!!! There is no good way of disabling inheritance of the configSections tag (As configSections must appear as the first tag in the Web.Config!). Microsoft issued a statement stating that it would be too complex to handle this appropriately using their given development methods. Simply, make sure that your sectiongroup name's don't clash with child apps. You can simply rename one of them if they do share the same name. Easy fix!


Method 2
You can also prevent inheritance by using clear and/or remove tags in the child's Web.Config to clear our any inherited values defined in the root Web.Config.
This is an example of a child app's Web.Config. It clears our any inherited connection strings defined by the root application. You then define the child app's connection strings BELOW the clear attribute. Note: Depending upon the Web.Config tag involved, you may need a remove attribute rather than a clear.
Code Snippet
  1. <?xml version="1.0"?>
  2. <configuration>
  3.     <connectionStrings>
  4.         <clear/>
  5.         <!-- Child config's connection strings -->
  6.     </connectionStrings>
  7. </configuration>
End of Code Snippet