• ASP.NET MVC custom binder for currency

    I can imagine that’s quite common problem. You have a double (or better decimal) value that you want to show formatted as a currency field. Lets assume we are storing the price data in an object like this: public class TestModel { public double NumberField { get; set; } public double CurrencyField { get; set; } } The MVC view is strongly typed with this TestModel. And the view model value is formatted like this: <%=Html.TextBox(“NumberField”, Model.NumberField)%> <%=Html.TextBox(“CurrencyField”, Model.CurrencyField.ToString(“c”))%> If you set the current culture to German-Swiss, for example in (base)controller like that: protected override void Initialize(System.Web.Routing.RequestContext requestContext) { base.Initialize(requestContext); string culture = "de-CH"; CultureInfo ci = CultureInfo.GetCultureInfo(culture); Thread.CurrentThread.CurrentCulture =…

  • Easiest way to have 2 submit button in one html form

    Here is the easiest way to have two (or more) submit buttons in one html form and to make them “do” something else. It is very helpful if you are planning to implement a toolbar like behavior. Example is from ASP.NET MVC but it does not matter. Since it uses JavaScript to dynamically change the action attribute of a form tag, it can be used everywhere. Here it is: <h2><%= Html.Encode(ViewData["Message"]) %></h2> <script language="javascript" type="text/javascript"> function ChangeFormAction(sender, url) { sender.form.action = url; } </script> <form method="post"> <input id="text" name="text" type="text" value="Hello from Action" /> <br /> <input type="submit" value="Go to action 1" onclick="ChangeFormAction(this, '/Home/Action1')" /> <input type="submit" value="Go to action…

  • Keep your lib folder up to date

    I’m keeping all the 3rd party assemblies that I use in my .NET projects in a separate library folder called lib. It is a part of my Subversion repository. In most of my project it is placed in linked repository (with svn:externals property). It lets my check out the whole project on any machine I like and it compiles right away. But be aware! In such scenario (and, well… in any other scenario as well) you have to keep your lib up to date. Take a look at this nasty bug. I was using the NUnit version 2.4.6.0 Take a look at this test method: [NUnit.Framework.Test] public void Test() {…

  • How to associate a file extension with a given file?

    (Windows) You can edit a file type in Explorer (XP: Tools –> Folder options, Vista: Start –> Default Programs). But lets say you want to do it automatically and you have to set an additional parameter to the program you want to start (I have still not figured it out how to define such parameter under Vista). The easiest way is to create a file with *.reg extension that contains this script: Red (cicstarter) – Schell command name Blue (startf) – Action name Brown (cicvlm.exe f=\”%1\”) – Application path (parameter inside f=) Green (.cic) – File extension You can manipulate the colored parts to feet your needs. Here is the…

  • SQL from Linq to Entity Framework

    Problem: how do I tell what SQL does Linq to Entity Framework generates? If you are using SQL Server its easy. You start the SQL Profiler and you are done. If you are using Oracle the task is not so easy. You can: 1. start the trace on the Oracle server and get the huge barely readable text file physically on the server and use TKPROF 2. look up the lat SQL query from session using the Enterprise Manager-Konsole 3. buy a 3rd party tool like FlexTracer to trace the OCI communication. Well yeah! Not fun. But there is another option inside Entity Framework itself! It is not quite obvious,…

  • Everyone is entitled to broad opinion

    I had an interesting dispute with my colleague weather to use only qualified type names in code or not. He insisted that we should not use the using directive to introduce a namespace but to write the fully qualified name every time it is necessary. I had quite opposite opinion but I agreed to give it a try. So I started to write public class MyClass { public static void Main() { System.Console.WriteLine("Foo"); } } instead of using System; public class MyClass { public static void Main() { Console.WriteLine("Foo"); } } Unfortunately over time it proofed no value to me. I was not happy with this rule, but I thought…

  • MTS 2008, Warsaw, Poland

    Microsoft Technology Summit 2008 (8th and 9th November), the biggest  Microsoft technology event in Poland went quite well. It was my first MTS and I was trying not to expect anything special. And indeed I haven’t experienced anything  earth shaking but altogether it was quite interesting and a good investment of time. I surely learned a few new things and meet interesting people there. I was astonished by the sheer number of attendees that gathered in there. I presume the old communist “castle” in the center of Warsaw the “Palace of Culture and Science” was the only place to accommodate this whole crowd, but it is not a cushy place.…

  • What null smaller than 0? Adventures with nullable types in .NET

    Lets say we have two nullable integers like this: int? i = null; int? j = 0; Is null smaller than 0 Console.WriteLine(i < j); False – no it is not. So probably null is greater than 0 Console.WriteLine(i > j); False – no it is not greater as well. All right! So null is equal 0. It has to be, JIT has no other choice, right? Console.WriteLine(i == j); Well False too! This two little fellows are not equal too. What? Is it raining frogs and we about to experience Armageddon? No! We have to use Nullable.Compare() and we will by back in normal world: switch (Nullable.Compare(i, j)) {…

  • Enum Factory

    I have a little tip for you. Let’s say you have an enumeration. You need a enumeration constant out of string variable. simply use System.Enum.Parse(). Here a small Snippet Compiler source code. using System; using System.Collections.Generic; public class MyClass { public enum FooBar { Foo, Bar, FooBar } public static void Main() { WL(FooBarFactory("Foo")); RL(); } public static FooBar FooBarFactory(string init) { try { return (FooBar)System.Enum.Parse(typeof(FooBar), init); } catch { // Do something throw; } } #region Helper methods private static void WL(object text, params object[] args) { Console.WriteLine(text.ToString(), args); } private static void RL() { Console.ReadLine(); } private static void Break() { System.Diagnostics.Debugger.Break(); } #endregion }

  • Silverlight for everybody

    My fist text for a Polish computer magazine NEXT hast just been published. It is called “Silverlight for everybody”. New publisher new challenge. I hope for a long lasting collaboration. In a mean time you can check my GeoCodedCalcualtor. A simple piece of Silverlight app that calculates the degrees, minutes, seconds form of latitude, longitude notation to a numeric notation and shows the translated coordinates using Google Static Maps Api. Enjoy!