Friday, March 22, 2013

Making the LinqDataSource control

It is a wonderful codes, make notes.


    public class Person
    {
        private String _name;
        private String _address;

        public string Address
        {
            get { return _address; }
            set { _address = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
    }


    public class PersonManager
    {
        private static readonly List<Person> _people = null;
        static PersonManager()
        {
            _people = new List<Person>
        {
            new Person{ Name = "Luis", Address="Fx"},
            new Person{ Name = "Luis2", Address="Fx"},
            new Person{ Name = "Luis3", Address="Fx"},
            new Person{ Name = "Luis4", Address="Fx2"}
        };
        }

        public IList<Person> People
        {
            get
            {
                return _people;
            }
        }
    }


<asp:LinqDataSource runat="server"
           ID="source"
           ContextTypeName="PersonManager"
           Where="Address==@address"
           TableName="People">
           <WhereParameters>
               <asp:Parameter Name="address" DefaultValue="Fx" />
           </whereParameters>    
</asp:LinqDataSource>

the original code is from the following link
http://msmvps.com/blogs/luisabreu/archive/2007/11/07/making-the-linqdatasource-control-work-against-an-ienumerable.aspx


    public class Class1
    {
        private static readonly List<Code> _code = null;

        static Class1()
        {
            DataClasses1DataContext db = new DataClasses1DataContext();
            var q = from c in db.Codes select c;
            _code = q.ToList<Code>();
        }

        public IList<Code> Code { get { return _code; } }
    }

Thursday, March 21, 2013

ASP.NET WebGrid and Web API

There are two link Get the Most out of WebGrid in ASP.NET MVC http://msdn.microsoft.com/en-us/magazine/hh288075.aspx Introduction to ASP.NET Web API http://www.codeproject.com/Articles/549152/Introduction-to-ASP-NET-Web-API

Multi Key in Dictionary

This is a very simple example, got from google search.
    
        static void Main(string[] args)
        {
            var lista = ListaClienteConta();
            //var chave = Tuple.Create(1, 1);
            //Console.WriteLine("Saldo selecionado é: {0}",
            //            lista[chave].ToString());
            //Console.WriteLine(lista.Keys+":"+lista.Values);
            foreach ( var pair in  lista)
            {
                Console.WriteLine("{0},{1}",pair.Value,pair.Key.ToString());
                Console.WriteLine("{0}",pair.Key.Item1);
            }
            Console.Read();
        }



        public static Dictionary<Tuple<int, int>, string> ListaClienteConta()
        {
            Dictionary<Tuple<int, int>, string> lista =
                                new Dictionary<Tuple<int, int>, string>();
           
            lista.Add(Tuple.Create(1, 2), "aa");
            lista.Add(Tuple.Create(3, 4), "bb");
           
            return lista;
        }

Friday, March 1, 2013

Get Decimal Places (Scale)

Get from google search:




      public double GetPrecision(string s)
        {
            NumberFormatInfo nfi = new NumberFormatInfo();
            string[] splitNumber = s.Split('.');
            if (splitNumber.Length &amp;gt; 1)
            {
                return 1 / Math.Pow(10, splitNumber[1].Length);
            }
            else
            {
                return 1;
            }
        }

        public int getscale(decimal dec)
        {
            uint[] bits = (uint[])(object)decimal.GetBits(dec);
            uint scale = (bits[3] &amp;gt;&amp;gt; 16) &amp;amp; 31;
            return (int)scale;
        }

        const int SIGN_MASK = ~Int32.MinValue;
        public static int GetDigits(Decimal value)
        {
            return (Decimal.GetBits(value)[3] &amp;amp; SIGN_MASK) &amp;gt;&amp;gt; 16;
        }

Currency (Money) in Javascript



            var Money = function (amount) {
                this.amount = amount;
            }
            Money.prototype.valueOf = function () {
                return Math.round(this.amount * 100) / 100;
            }

            var m = new Money(50.42355446);
            var n = new Money(30.342141);

            alert(m.amount + n.amount);
            alert(m + n);

Wednesday, February 6, 2013

Get list of DataField in gridview


String boundFields = String.Empty;

for (int i = 0; i < grd.Columns.Count; i++)
{
    DataControlField field = grd.Columns[i];
    BoundField bfield = field as BoundField;

    if (bfield != null)
        boundFields += bfield.DataField + ",";
}
boundFields = boundFields.TrimEnd(','); 

The code is from the following link:
http://stackoverflow.com/questions/6449832/get-list-of-datafield-in-gridview