Common required functions of C#
To Convert Date to String and vice e versa.
To Convert List to DataTable
To Write the Error of the Project.
To Convert Date to String and vice e versa.
public static string ToDate2String(DateTime dt)
{
string DateFormat = "MM/dd/yyyy";
return dt.ToString(DateFormat);
}
public static DateTime ToString2Date(string dt)
{
string DateFormat = "MM/dd/yyyy";
return DateTime.ParseExact(dt, DateFormat, CultureInfo.InvariantCulture);
}
{
string DateFormat = "MM/dd/yyyy";
return dt.ToString(DateFormat);
}
public static DateTime ToString2Date(string dt)
{
string DateFormat = "MM/dd/yyyy";
return DateTime.ParseExact(dt, DateFormat, CultureInfo.InvariantCulture);
}
To Convert List to DataTable
public static DataTable ListToDataTable<T>(IEnumerable<T> list)
{
DataTable table = new DataTable();
if (list != null)
{
PropertyDescriptorCollection properties =
TypeDescriptor.GetProperties(typeof(T));
foreach (PropertyDescriptor prop in properties)
table.Columns.Add(
prop.Name,
(prop.PropertyType.IsGenericType &&
prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
? Nullable.GetUnderlyingType(prop.PropertyType)
: prop.PropertyType
);
foreach (T item in list)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
table.Rows.Add(row);
}
}
return table;
}
{
DataTable table = new DataTable();
if (list != null)
{
PropertyDescriptorCollection properties =
TypeDescriptor.GetProperties(typeof(T));
foreach (PropertyDescriptor prop in properties)
table.Columns.Add(
prop.Name,
(prop.PropertyType.IsGenericType &&
prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
? Nullable.GetUnderlyingType(prop.PropertyType)
: prop.PropertyType
);
foreach (T item in list)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
table.Rows.Add(row);
}
}
return table;
}
To Write the Error of the Project.
public static void WriteError(Exception ex)
{
string err = null;
try
{
string path = "~/ErrorLogs/" + DateTime.Today.ToString("dd-MMM-yy") + ".txt";
if (!File.Exists(HttpContext.Current.Server.MapPath(path)))
File.Create(HttpContext.Current.Server.MapPath(path)).Close();
using (StreamWriter w = File.AppendText(HttpContext.Current.Server.MapPath(path)))
{
System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(ex, true);
w.WriteLine("\r\nLog Entry : ");
w.WriteLine("{0}", DateTime.Now.ToString(CultureInfo.InvariantCulture));
err = "Error in: " + HttpContext.Current.Request.Url.ToString() + Environment.NewLine +
"File Name : " + trace.GetFrame(0).GetFileName() + Environment.NewLine +
"Line : " + trace.GetFrame(0).GetFileLineNumber() + Environment.NewLine +
"Column: " + trace.GetFrame(0).GetFileColumnNumber() + Environment.NewLine +
"Error Message:" + ex.Message + Environment.NewLine +
"TargetSite:" + ex.TargetSite.ToString();
w.WriteLine(err);
w.WriteLine("__________________________");
w.Flush();
w.Close();
}
}
catch (Exception exc)
{
WriteError(exc);
}
}
{
string err = null;
try
{
string path = "~/ErrorLogs/" + DateTime.Today.ToString("dd-MMM-yy") + ".txt";
if (!File.Exists(HttpContext.Current.Server.MapPath(path)))
File.Create(HttpContext.Current.Server.MapPath(path)).Close();
using (StreamWriter w = File.AppendText(HttpContext.Current.Server.MapPath(path)))
{
System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(ex, true);
w.WriteLine("\r\nLog Entry : ");
w.WriteLine("{0}", DateTime.Now.ToString(CultureInfo.InvariantCulture));
err = "Error in: " + HttpContext.Current.Request.Url.ToString() + Environment.NewLine +
"File Name : " + trace.GetFrame(0).GetFileName() + Environment.NewLine +
"Line : " + trace.GetFrame(0).GetFileLineNumber() + Environment.NewLine +
"Column: " + trace.GetFrame(0).GetFileColumnNumber() + Environment.NewLine +
"Error Message:" + ex.Message + Environment.NewLine +
"TargetSite:" + ex.TargetSite.ToString();
w.WriteLine(err);
w.WriteLine("__________________________");
w.Flush();
w.Close();
}
}
catch (Exception exc)
{
WriteError(exc);
}
}
No comments:
Post a Comment