14 Haziran 2013 Cuma

Converting DataTable to List Object

source retrieved from stackoverflow

// your namespace{

  public static class DataTableExtensions
    {
        private static Dictionary<Type, IList<PropertyInfo>> typeDictionary = new Dictionary<Type, IList<PropertyInfo>>();
        public static IList<PropertyInfo> GetPropertiesForType<T>()
        {
                var type = typeof(T);
               
            if(!typeDictionary.ContainsKey(typeof(T)))
                {
                    typeDictionary.Add(type, type.GetProperties().ToList());
                }
                
            return typeDictionary[type];
        }

        /// <summary>
        /// usage  yourDataTable.ToList<YourType>()
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="table"></param>
        /// <returns>List<YourType></returns>
        public static IList<T> ToList<T>(this DataTable table) where T : new()
        {
            IList<PropertyInfo> properties = GetPropertiesForType<T>();
            IList<T> result = new List<T>();

            foreach (var row in table.Rows)
            {
                var item = CreateItemFromRow<T>((DataRow)row, properties);
                result.Add(item);
            }

            return result;
        }

        private static T CreateItemFromRow<T>(DataRow row, IList<PropertyInfo> properties) where T : new()
        {
            T item = new T();
            foreach (var property in properties)
            {
                property.SetValue(item, row[property.Name], null);
            }
            return item;
        }

    }

// }


Ternary for checking null for all type

using System;

public static class DataReaderExtensions
{
    public static bool IsDBNull(this IDataReader dataReader, string columnName)
    {
        return dataReader[columnName] == DBNull.Value;
    }
}

checking data reader is null

using System;

public static class DataReaderExtensions
{
    public static bool IsDBNull(this IDataReader dataReader, string columnName)
    {
        return dataReader[columnName] == DBNull.Value;
    }
}

by this extension method you can check datareader column whether null or not

13 Haziran 2013 Perşembe

changing div css class with c#

CODE BEHIND

divMessage.Attributes.Add("class", "info");




CSS CLASS

.
.
.

.info {
    color: #00529B;
    background-color: #BDE5F8;
    background-image: url('info.png');
}

.
.
.

evren pehlivan yazılım

evren pehlivan yazılım, evren pehlivan yazılım geliştirici