Thursday, September 30, 2010

ASP.NET IIS Errors troubleshooting

If you have this kind of error when you tried to run your ASP.NET app on IIS


HTTP Error 403.14 - Forbidden

The Web server is configured to not list the contents of this directory.


or this:

HTTP Error 500.21 - Internal Server Error

Handler "BlockViewHandler" has a bad module "ManagedPipelineHandler" in its module list


just do this:
Windows Logo > Run

type this:
cmd.exe

then press:
Ctrl+Shift+Enter

that shortcut key triggers running cmd.exe with an Administrator rights

then type this on terminal:
%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i

Sunday, September 19, 2010

If the input is optional, make it obvious in code

public static class Helper
{
    public static string CheckOverlookedValidation(object obj, string propName)
    {
        return Helper.CheckOverlookedValidation(obj, propName, null);
    }


    public static string CheckOverlookedValidation(object obj, string propName, params string[] exceptPropNames)
    {

        if (exceptPropNames != null)
        {
            foreach(string epn in exceptPropNames)
            {
                if (epn == propName)
                    return null;
            }
        }

        Type t = obj.GetType();

        PropertyInfo pi = t.GetProperty(propName);

        bool isNullable = !pi.PropertyType.IsValueType;

        if (!isNullable)
            if (pi.PropertyType.IsGenericType 
                && pi.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                isNullable = true;
            
        if (isNullable)
        {  
            object value = t.InvokeMember(propName, System.Reflection.BindingFlags.GetProperty, null, obj, null);
                
            string programmerAlert = "{0} has no custom validation. Alert the Programmer";

            if (value == null)
                return string.Format(programmerAlert, propName);
            else if (pi.PropertyType == typeof(string) && string.IsNullOrWhiteSpace((string)value))
                return string.Format(programmerAlert, propName);
            else
                return null;
        }

        return null;
    }
}


Friday, September 17, 2010

Logic Puzzle


Two days ago Michael was nine years old. Next year he will be 12 years old. What day of the year is his birthday?


Answer: December 31

The base date is January 1 2010. Two days ago(December 30 2009), Michael's age is 9

January 1 2010 = 10
December 31 2009 = 10
December 30 2009 = 9

Michael's age of December 31 2010 is 11
Next year, 2011 (December 31 2011), Michael's age is 12



Source: http://wiki.answers.com/Q/Two_days_ago_Michael_was_nine_years_old._Next_year_he_will_be_12_years_old._What_day_of_the_year_is_his_birthday

Saturday, September 4, 2010

Fibonacci using SQL






An exercise of logic, thinking of solution on-the-fly:

create or replace function generate_fib(_count int) returns table(n numeric)
as
$$
 
with recursive fib(cnt, a, b) as
(
 select 1, 0::numeric, 1::numeric
 union all
 select cnt + 1, b, a + b from fib where cnt <= $1 - 1
)
select a from fib;

$$ language 'sql';
 
select n from generate_fib(1000); -- will generate 1,000 fibonacci