Chorn Sokun's Weblog

Conquer inner fear, push it to the limit!

Posts Tagged ‘.NET

Rediscover WinForm, landing Alien base?

leave a comment »

Simple scenario each time a province change, district, commune & village will reset, not a big deal for me now a day to have this scenario on the web application but hey look it WinForm :( let see

I started from Form Load event cbProvince combobox get binded then wire cbProvince with SelectedIndexChanged event handler to load list of district into cbDistrict combobox and so on the code look more or less like this


public void Form_Load(object sender, System.EventArgs e){
  // binding cbProvince
}

public void cbProvince_SelectedIndexChanged(object sender, EventArgs e){
  //... binding the cbDistrict ComboBox using current selected value from cbProvince
}

public void cbDistrict_SelectedIndexChanged(object sender, EventArgs e){
  //... binding the cbCommune ComboBox using current selected value from cbDistrict
}

Sound good all event wired hit F5 to try out. Guess what happen? it slow & AND finally throw an exception !!! but how? years passed and this behavior didn’t change a bit

// flag state
private bool formReady = false;
public void Form_Load(object sender, System.EventArgs e){
  // binding cbProvince
  formReady = true;
}

public void cbProvince_SelectedIndexChanged(object sender, EventArgs e){
  if(!formReady) return; // this is the trick don't fire the event
  formReady = false;
  //... binding the cbDistrict ComboBox using current selected value from cbProvince
  formReady = true;
}
public void cbDistrict_SelectedIndexChanged(object sender, EventArgs e){
  if(!formReady) return; // this is the trick don't fire the event
  formReady = false;
  //... binding the cbCommune ComboBox using current selected value from cbDistrict
  formReady = true;
}
Advertisements

Written by Chorn Sokun

September 29, 2009 at 10:50 pm

Convert IList to List<T>

with 7 comments

Follow up with my NHibernate Series today I have another post talking about one of my favorite Util’s method.

This come from my heavy use of NHibernate classes such as DetachedCriteria, IMultiCriteria and I love to call on it .SetResultTransformer method to transform result into strong type; however its .List() method return an IList which doesn’t uncomforted me much because I addicted to generic & strong type object.

public static IList<T> ConvertToListOf<T>(IList iList)
{
  IList<T> result = new List<T>();
  foreach (T value in iList)
    result.Add(value);

  return result;
}

With this little method it allow me to work with generic type again and that is sweet ;)

EDIT: hmm, I just discovered something interesting actually I don’t really need this at all I can just manipulate iList element through foreach().

namespace ConsoleApplication1
{
  using System;
  using System.Collections;
  using System.Collections.Generic;
  using System.Diagnostics;

  internal class Program
  {
    private static void Main(string[] args)
    {
      IList x = new ArrayList();

      for(int i = 0; i < 100000; i++)
      {
        x.Add(new Person("First #" + i, "Last #" + i));
      }

      Stopwatch watch1 = new Stopwatch();
      watch1.Start();
      foreach(Person person in x)
      {
        Console.Write(person.FirstName);
        Console.Write(person.LastName);
      }
      watch1.Stop();

      Stopwatch watch2 = new Stopwatch();
      watch2.Start();
      IList<Person> persons = ConvertToListOf<Person>(x);
      foreach(Person person in persons)
      {
        Console.Write(person.FirstName);
        Console.Write(person.LastName);
      }
      watch2.Stop();

      Console.Clear();

      Console.WriteLine("Watch #1: {0}\r\nWatch #2: {1}", watch1.Elapsed, watch2.Elapsed);
      Console.Read();
    }

    public static IList<T> ConvertToListOf<T>(IList iList)
    {
      IList<T> result = new List<T>();
      foreach(T value in iList)
      {
        result.Add(value);
      }

      return result;
    }
  }

  public class Person
  {
    public string FirstName;
    public string LastName;

    public Person(string firstName, string lastName)
    {
      FirstName = firstName;
      LastName = lastName;
    }
  }
}

However it come down to an interesting measurement issue
* Pass through *
Watch #1: 00:00:00.0075761
Watch #2: 00:00:00.0131754

* Pass through & read field *
Watch #1: 00:00:06.2585406 (process direct)
Watch #2: 00:00:06.3542894 (convert first)

Written by Chorn Sokun

May 27, 2008 at 10:47 am

Debugging failed because integrated Windows authentication is not enabled

leave a comment »

On of the annoying error message while trying to debug my ASP.NET application.

The fact, I just did a clean install my machine (winxp) and manually create virtual directory from IIS and epected it just work, but no I need to perform a few additional step to make thing work.

Enable Integrated Windows Authentication on the local IIS

  1. Open IIS Manager (Internbet Information Services)
  2. Right-click the web site (in case you run it locally you only have Default web site) and pick Properties
  3. Choose “Directory Security” tab and click Edit on “Anonymous access and authentication control
  4. In the opening window, uncheck “Allow Anonymous access” and check “Integrated Windows Authentication” (allowing anonymous can make that you don’t have enough permissions to debug)

Hope I don’t forget it again ;)

Written by Chorn Sokun

January 9, 2007 at 10:14 am

Posted in Tips & Tricks

Tagged with ,

Great power come great responsibility

leave a comment »

Coding in .NET for about 2 years, I got hooked up with the framework damn!
I am not gonna talk about cross platform challenges that Java guy raised. I will do it another day. But hell, what kind of application you want to build? It remind me of an old expression from Microsoft “Where do you wan to go today?” :)

How about creating a ticket system allow user to plug their webcam and capture image do a quick selection (face of cause) and then add it to card tray send cards direct to printer.
How many day? lines of code? will you need to write in C/C++.

At the end of the day, I am the on who scratch my head trying to get thing to work it such a great experience. I hope that my client would enjoy using it ;)

Written by Chorn Sokun

November 16, 2006 at 8:24 am

Posted in Development

Tagged with , ,

Loading *.rdlc stored outside the assembly

leave a comment »

I am back with the ReportViewer control shipped in .NET 2.0
What is the problem again? well it not much I just want to share about how I can load the *.rdlc file stored outside the Assembly.

Okay, let say my application have the following structure:

By store the report separate from the BillionDollarApp.exe (not embed *.rdlc as an assembly resource) it allow me to make change to the report without having to recompile my app.

To load the report file:

string stRDLCFileName = "Report1.rdlc";
this.reportViewer1.LocalReport.ReportPath = "Reports\\" + stRDLCFileName;
this.reportViewer1.RefreshReport();

Until then it all for now ;)

Written by Chorn Sokun

October 17, 2006 at 3:06 pm