Quick Tip : List available encodings

Here is a way to list the available encodings for a version of the .NET Framework


using System.Text;

 


EncodingInfo[] encodings = Encoding.GetEncodings();

Here is a piece of code to output the encoding info in an html file (as a table for readability)


var encodings = (from e in Encoding.GetEncodings()
 orderby e.Name
 select string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>", e.CodePage, e.Name, e.DisplayName)
 ).ToList();

// add html part infos
 encodings.Insert(0, "<html><head><title>Encodings</title><style>table {width:100%;} table tr:nth-child(2n){background:#ddd;}</style></head><body><h1>Encodings list</h1><table><tr><th>Code</th><th>Name</th><th>DisplayName</th></tr>");
 encodings.Add("</table></body></html>");
 File.WriteAllLines(@"c:\encodings.html", encodings);

The result for the .NET FX 4.5 :

CodeNameDisplayName
708ASMO-708Arabic (ASMO 708)
950big5Chinese Traditional (Big5)
21025cp1025IBM EBCDIC (Cyrillic Serbian-Bulgarian)
866cp866Cyrillic (DOS)
875cp875IBM EBCDIC (Greek Modern)
50221csISO2022JPJapanese (JIS-Allow 1 byte Kana)
720DOS-720Arabic (DOS)
862DOS-862Hebrew (DOS)
51936EUC-CNChinese Simplified (EUC)
51932euc-jpJapanese (EUC)
20932EUC-JPJapanese (JIS 0208-1990 and 0212-1990)
51949euc-krKorean (EUC)
54936GB18030Chinese Simplified (GB18030)
936gb2312Chinese Simplified (GB2312)
52936hz-gb-2312Chinese Simplified (HZ)
858IBM00858OEM Multilingual Latin I
20924IBM00924IBM Latin-1
1047IBM01047IBM Latin-1
1140IBM01140IBM EBCDIC (US-Canada-Euro)
1141IBM01141IBM EBCDIC (Germany-Euro)
1142IBM01142IBM EBCDIC (Denmark-Norway-Euro)
1143IBM01143IBM EBCDIC (Finland-Sweden-Euro)
1144IBM01144IBM EBCDIC (Italy-Euro)
1145IBM01145IBM EBCDIC (Spain-Euro)
1146IBM01146IBM EBCDIC (UK-Euro)
1147IBM01147IBM EBCDIC (France-Euro)
1148IBM01148IBM EBCDIC (International-Euro)
1149IBM01149IBM EBCDIC (Icelandic-Euro)
37IBM037IBM EBCDIC (US-Canada)
1026IBM1026IBM EBCDIC (Turkish Latin-5)
20273IBM273IBM EBCDIC (Germany)
20277IBM277IBM EBCDIC (Denmark-Norway)
20278IBM278IBM EBCDIC (Finland-Sweden)
20280IBM280IBM EBCDIC (Italy)
20284IBM284IBM EBCDIC (Spain)
20285IBM285IBM EBCDIC (UK)
20290IBM290IBM EBCDIC (Japanese katakana)
20297IBM297IBM EBCDIC (France)
20420IBM420IBM EBCDIC (Arabic)
20423IBM423IBM EBCDIC (Greek)
20424IBM424IBM EBCDIC (Hebrew)
437IBM437OEM United States
500IBM500IBM EBCDIC (International)
737ibm737Greek (DOS)
775ibm775Baltic (DOS)
850ibm850Western European (DOS)
852ibm852Central European (DOS)
855IBM855OEM Cyrillic
857ibm857Turkish (DOS)
860IBM860Portuguese (DOS)
861ibm861Icelandic (DOS)
863IBM863French Canadian (DOS)
864IBM864Arabic (864)
865IBM865Nordic (DOS)
869ibm869Greek, Modern (DOS)
870IBM870IBM EBCDIC (Multilingual Latin-2)
20871IBM871IBM EBCDIC (Icelandic)
20880IBM880IBM EBCDIC (Cyrillic Russian)
20905IBM905IBM EBCDIC (Turkish)
20838IBM-ThaiIBM EBCDIC (Thai)
50220iso-2022-jpJapanese (JIS)
50222iso-2022-jpJapanese (JIS-Allow 1 byte Kana – SO/SI)
50225iso-2022-krKorean (ISO)
28591iso-8859-1Western European (ISO)
28603iso-8859-13Estonian (ISO)
28605iso-8859-15Latin 9 (ISO)
28592iso-8859-2Central European (ISO)
28593iso-8859-3Latin 3 (ISO)
28594iso-8859-4Baltic (ISO)
28595iso-8859-5Cyrillic (ISO)
28596iso-8859-6Arabic (ISO)
28597iso-8859-7Greek (ISO)
28598iso-8859-8Hebrew (ISO-Visual)
38598iso-8859-8-iHebrew (ISO-Logical)
28599iso-8859-9Turkish (ISO)
1361JohabKorean (Johab)
20866koi8-rCyrillic (KOI8-R)
21866koi8-uCyrillic (KOI8-U)
949ks_c_5601-1987Korean
10000macintoshWestern European (Mac)
932shift_jisJapanese (Shift-JIS)
20127us-asciiUS-ASCII
1200utf-16Unicode
1201utf-16BEUnicode (Big-Endian)
12000utf-32Unicode (UTF-32)
12001utf-32BEUnicode (UTF-32 Big-Endian)
65000utf-7Unicode (UTF-7)
65001utf-8Unicode (UTF-8)
1250windows-1250Central European (Windows)
1251windows-1251Cyrillic (Windows)
1252Windows-1252Western European (Windows)
1253windows-1253Greek (Windows)
1254windows-1254Turkish (Windows)
1255windows-1255Hebrew (Windows)
1256windows-1256Arabic (Windows)
1257windows-1257Baltic (Windows)
1258windows-1258Vietnamese (Windows)
874windows-874Thai (Windows)
20000x-Chinese-CNSChinese Traditional (CNS)
20002x-Chinese-EtenChinese Traditional (Eten)
20001x-cp20001TCA Taiwan
20003x-cp20003IBM5550 Taiwan
20004x-cp20004TeleText Taiwan
20005x-cp20005Wang Taiwan
20261x-cp20261T.61
20269x-cp20269ISO-6937
20936x-cp20936Chinese Simplified (GB2312-80)
20949x-cp20949Korean Wansung
50227x-cp50227Chinese Simplified (ISO-2022)
20833x-EBCDIC-KoreanExtendedIBM EBCDIC (Korean Extended)
29001x-EuropaEuropa
20105x-IA5Western European (IA5)
20106x-IA5-GermanGerman (IA5)
20108x-IA5-NorwegianNorwegian (IA5)
20107x-IA5-SwedishSwedish (IA5)
57006x-iscii-asISCII Assamese
57003x-iscii-beISCII Bengali
57002x-iscii-deISCII Devanagari
57010x-iscii-guISCII Gujarati
57008x-iscii-kaISCII Kannada
57009x-iscii-maISCII Malayalam
57007x-iscii-orISCII Oriya
57011x-iscii-paISCII Punjabi
57004x-iscii-taISCII Tamil
57005x-iscii-teISCII Telugu
10004x-mac-arabicArabic (Mac)
10029x-mac-ceCentral European (Mac)
10008x-mac-chinesesimpChinese Simplified (Mac)
10002x-mac-chinesetradChinese Traditional (Mac)
10082x-mac-croatianCroatian (Mac)
10007x-mac-cyrillicCyrillic (Mac)
10006x-mac-greekGreek (Mac)
10005x-mac-hebrewHebrew (Mac)
10079x-mac-icelandicIcelandic (Mac)
10001x-mac-japaneseJapanese (Mac)
10003x-mac-koreanKorean (Mac)
10010x-mac-romanianRomanian (Mac)
10021x-mac-thaiThai (Mac)
10081x-mac-turkishTurkish (Mac)
10017x-mac-ukrainianUkrainian (Mac)

Incoming search terms:

  • scumitz koi8
  • terminal available encodings list
  • Available encodings list

Quick tip : redirect the console output to a file

In a console application, we use a lot the Console.WriteLine method.

We can redirect the output of the WriteLine method easily :


var consoleOut = Console.Out; // save the current output stream to be able to re-use it later

Console.SetOut(File.CreateText(@"C:\consoleout.txt")); // change the output stream for a file
 Console.WriteLine("a sample text"); // write some information

Console.SetOut(consoleOut); // reset the output to the console.

MVC and Entity Framework : Unable to retrieve the metadata for the model

Let’s say you are developing an MVC application and you want to add a generated controller with the associated views.

You do this:

  1. Setting the controller’s name
  2. Choosing the template: mvc controller with read/write actions and views, using Entity Framework
  3. Choosing the model class
  4. Choosing the data context
  5. Clicking “Add”

image001

It’s possible that you get this error:

image002

The solution is quite simple:

Just go in the web.config file and comment the connection string

image004

Then try again

image005

It works!

Don’t forget to uncomment your connection string and you are done.

Incoming search terms:

  • unable to retrieve metadata for model class
  • MVCandEntityFramework:Unabletoretrievethemetadataforthemodel
  • unable to retrieve metadata for
  • unable to retrieve metadata for model
  • unable to retrieve metadata for mvc
  • unable to retrieve metadata
  • entity framework unable to retrieve metadata
  • asp net mvc unable to retrieve metadata
  • mvc unable to retrieve metadata
  • yhs-fullyhosted_003

Quick tip: list all poco’s properties and their values

Let’s say you have a poco and you want to list all its properties whith their values for debug purpose (or other purposes : logs, …)

You can write something like this :


string porperties = "property1 : " + this.property1;
porperties+="property : " + this.property2;
// ..
porperties+="propertyn : " + this.propertyn;

but it’s long and painful, plus you can forget some properties

The reflection can help us here :


string porperties = "";
 foreach (System.Reflection.PropertyInfo f in this.GetType().GetProperties())
 {
porperties += f.Name + " : " + f.GetValue(this, null) + Environment.NewLine;
 }

And if you want to be a bit clever, you create a base object and redefine the ‘ToString() method like this:


public class POCOBase
{
public override string ToString()
{
string ret = "";
foreach (System.Reflection.PropertyInfo f in this.GetType().GetProperties())
{
ret += f.Name + " : " + f.GetValue(this, null) + Environment.NewLine;
  }
  return ret;
 }
}

Then just inherit from POCOBase and call the ToString() method

The output should be something like this :

AccountID : 10
ExternalREF : 090xfe
FIRSTNAME : USER 1
LASTNAME : TEST
PHONE : 00044440000
RegisterDate : 30/09/2009 16:20:02
Remark :

I think it’s quick, easy and useful

Bring a bit of the Subsonic power to Entity Framework by adding automatic audit and logical delete fields

Here is the code to do the same thing than in my previous article “Bring a bit of the Subsonic power to Linq to sql by adding automatic audit and logical delete fields”


namespace YouNamespace.DAL
{
 using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.Data;
 using System.Data.Common;
 using System.Data.Objects;
 using System.Linq;

 public partial class YOURCONTEXTEntities
 {
 /// <summary>
 /// System fields for automatic audit and logical delete
 /// </summary>
 private struct SystemFields
 {
 public const string CreatedOn = "CREATEDON";
 public const string ModifiedOn = "MODIFIEDON";
 public const string CreatedBy = "CREATEDBY";
 public const string ModifiedBy = "MODIFIEDBY";
 public const string IsDeleted = "ISDELETED";
 }

 /// <summary>
 /// Overriding the SaveChanges method to automaticaly set system fields if any.
 /// </summary>
 /// <param name="options"></param>
 /// <returns></returns>
 public override int SaveChanges(System.Data.Objects.SaveOptions options)
 {
 IEnumerable<ObjectStateEntry> newEntries = this.ObjectStateManager.GetObjectStateEntries(EntityState.Added);

 foreach (ObjectStateEntry entry in newEntries)
 {
 ReadOnlyCollection<FieldMetadata> fieldsMetaData = entry.CurrentValues
 .DataRecordInfo.FieldMetadata;

 FieldMetadata createdOnField = fieldsMetaData
 .Where(f => string.Equals(f.FieldType.Name, SystemFields.CreatedOn, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

 if (createdOnField.FieldType != null)
 {
 entry.CurrentValues.SetValue(createdOnField.Ordinal, DateTime.Now);
 }

 FieldMetadata createdByField = fieldsMetaData
 .Where(f => string.Equals(f.FieldType.Name, SystemFields.CreatedBy, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

 if (createdByField.FieldType != null)
 {
 entry.CurrentValues.SetValue(createdByField.Ordinal, "Sam");
 }

 FieldMetadata deletedField = fieldsMetaData
 .Where(f => string.Equals(f.FieldType.Name, SystemFields.IsDeleted, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

 if (deletedField.FieldType != null)
 {
 entry.CurrentValues.SetValue(deletedField.Ordinal, false);
 }
 }

 IEnumerable<ObjectStateEntry> modifiedEntries = this.ObjectStateManager.GetObjectStateEntries(EntityState.Modified);
 foreach (ObjectStateEntry entry in modifiedEntries)
 {
 ReadOnlyCollection<FieldMetadata> fieldsMetaData = entry.CurrentValues
 .DataRecordInfo.FieldMetadata;

 FieldMetadata createdOnField = fieldsMetaData
 .Where(f => string.Equals(f.FieldType.Name, SystemFields.ModifiedOn, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

 if (createdOnField.FieldType != null)
 {
 entry.CurrentValues.SetValue(createdOnField.Ordinal, DateTime.Now);
 }

 FieldMetadata createdByField = fieldsMetaData
 .Where(f => string.Equals(f.FieldType.Name, SystemFields.ModifiedBy, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

 if (createdByField.FieldType != null)
 {
 entry.CurrentValues.SetValue(createdByField.Ordinal, "Sam");
 }
 }

 IEnumerable<ObjectStateEntry> deletedEntries = this.ObjectStateManager.GetObjectStateEntries(EntityState.Deleted);
 foreach (ObjectStateEntry entry in deletedEntries)
 {
 // change from deleted to modified (!important)
 this.ObjectStateManager.ChangeObjectState(entry.Entity, EntityState.Modified);

 ReadOnlyCollection<FieldMetadata> fieldsMetaData = entry.CurrentValues
 .DataRecordInfo.FieldMetadata;

 FieldMetadata deletedField = fieldsMetaData
 .Where(f => string.Equals(f.FieldType.Name, SystemFields.IsDeleted, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

 if (deletedField.FieldType != null)
 {
 entry.CurrentValues.SetValue(deletedField.Ordinal, true);
 }
else
 {
 // change back from modified to deleted (!important)
 this.ObjectStateManager.ChangeObjectState(entry.Entity, EntityState.Deleted);
 }
 }

 return base.SaveChanges(options);
 }
 }
}

Incoming search terms:

  • entry currentvalues
  • subsonic audit
  • subsonic createdon
  • subsonic entity framework
  • subsonic Leave a Reply comment -captcha