Sunday, May 2, 2010

The Object base class

In the .NET Framework, all types are derived from System.Object. That relationship helps establish the common type system used throughout the .NET Framework

Using Value Types

The simplest types in the .NET Framework, primarily numeric and boolean types, are
value types. Value types are variables that contain their data directly instead of containing
a reference to the data stored elsewhere in memory. Instances of value types are
stored in an area of memory called the stack, where the runtime can create, read,
update, and remove them quickly with minimal overhead.

There are three general value types:
  • Built-in types
  • User-defined types
  • Enumerations
Each of these types is derived from the System.ValueType base type.

Thursday, April 29, 2010

GridView Command

(1)By ID

CommandArgument='<%# Bind("Pk_CategoryId") %>'

(2)By Data Key


DataKeyNames="Pk_CategoryId"

CommandArgument='<%# Convert.ToString(Container.DataItemIndex) %>'


RowCommand Event


int id = Convert.ToInt32(e.CommandArgument);
if (id >= Convert.ToInt32(grdAreaVillage.PageSize))
{
id = id - (Convert.ToInt32(grdAreaVillage.PageSize) * Convert.ToInt32(grdAreaVillage.PageIndex));
}
decimal AreaVillageId = Convert.ToDecimal(grdAreaVillage.DataKeys[id]["Pk_CategoryId"]);



Fromat Date

Text='<%# Convert.ToDateTime(Eval("AnswerDate")).ToString("MMMM d, yyyy") %>'

LINQ Compile Query

private static Func<TestDataContext, IEnumerable>

ParentRecords =

CompiledQuery.Compile<TestDataContext, IEnumerable>((TestDataContext db) =>

from m in db.Tablenames

select new { m.CategoryName, m.fk_CategoryId, m.Pk_CategoryId });

Object Array To DataTable

#region Object Array to DataTable

/// Method to Convert Datatable from object Array.

public static DataTable ToDatatable(this Object[] array)

{

PropertyInfo[] properties = array.GetType().GetElementType().GetProperties();

DataTable dt = CreateDataTable(properties);

if (array.Length != 0)

{

foreach (object o in array)

FillData(properties, dt, o);

}

return dt;

}




/// Method To Create total column of datatable.

private static DataTable CreateDataTable(PropertyInfo[] properties)

{

DataTable dt = new DataTable();

DataColumn dc = null;

foreach (PropertyInfo pi in properties)

{

dc = new DataColumn();

dc.ColumnName = pi.Name;

//dc.DataType = pi.PropertyType;

dt.Columns.Add(dc);

}

return dt;

}



/// Method for Fill data in DataTable.


private static void FillData(PropertyInfo[] properties, DataTable dt, Object o)

{

DataRow dr = dt.NewRow();

foreach (PropertyInfo pi in properties)

{

dr[pi.Name] = pi.GetValue(o, null);

}

dt.Rows.Add(dr);

}

#endregion

List To DataTable

#region List To DataTable

public static DataTable ToDataTable(this List items)

{

var tb = new DataTable(typeof(T).Name);

PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

foreach (PropertyInfo prop in props)

{

Type t = GetCoreType(prop.PropertyType);

tb.Columns.Add(prop.Name, t);

}

foreach (T item in items)

{

var values = new object[props.Length];

for (int i = 0; i <>

{

values[i] = props[i].GetValue(item, null);

}

tb.Rows.Add(values);

}

return tb;

}



///

/// Determine of specified type is nullable

///

public static bool IsNullable(Type t)

{

return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));

}



///

/// Return underlying type if type is Nullable otherwise return the type

///

public static Type GetCoreType(Type t)

{

if (t != null && IsNullable(t))

{

if (!t.IsValueType)

{

return t;

}

else

{

return Nullable.GetUnderlyingType(t);

}

}

else

{

return t;

}

}

#endregion

IEnumerable to DataTable


public static DataTable ToDataTable(this IEnumerable ien)
{
DataTable dt = new DataTable();
foreach (object obj in ien)
{
Type t = obj.GetType();
PropertyInfo[] pis = t.GetProperties();
if (dt.Columns.Count == 0)
{
foreach (PropertyInfo pi in pis)
{
  
Type propType = pi.PropertyType;

                    // Is it a nullable type? Get the underlying type
                    if (propType.IsGenericType && propType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
                     
   propType = new System.ComponentModel.NullableConverter(propType).UnderlyingType;

                    dt.Columns.Add(pi.Name, propType);

}
}
DataRow dr = dt.NewRow();
foreach (PropertyInfo pi in pis)
{
object value = pi.GetValue(obj, null);
dr[pi.Name] = value;
}
dt.Rows.Add(dr);
}
return dt;
}