public static DataTable ConvertToDataTable<T>(IList<T> data)
{
PropertyDescriptorCollection properties =
TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
foreach (PropertyDescriptor prop in properties)
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
foreach (T item in data)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
table.Rows.Add(row);
}
return table;
}
DAL
#region Insert Bulk Housing Allowance to a Payment Group
public int AddBulkPaymentGroup(List<AddBulkPaymentGroup> addBulkPaymentGroup)
{
int returnParam = 0;
try
{
if (sqlcon.State == ConnectionState.Closed)
{
sqlcon.Open();
}
// lets begin a transaction here
transaction = sqlcon.BeginTransaction();
DataTable dt = SqlHelpers.ConvertToDataTable<AddBulkPaymentGroup>(addBulkPaymentGroup);
cmd = new SqlCommand(storedProcedure_AddBulkPaymentGroup, sqlcon, transaction);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter tvpParam = cmd.Parameters.AddWithValue("@BulkPaymentGroupType", dt);
tvpParam.SqlDbType = SqlDbType.Structured;
returnParam= cmd.ExecuteNonQuery();
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
returnParam = 0;
MOACommons.Utils.ErrorLog.WriteLog(ex, this.GetType().ToString() + "-" + System.Reflection.MethodBase.GetCurrentMethod().Name);
}
finally
{
sqlcon.Close();
}
return returnParam;
}
#endregion
No comments:
Post a Comment