Code for parsing Date / Time formats
using System;
using System.Globalization;
namespace ExampleCode
{
public class ExampleCode
{
static void Main()
{
try
{
CultureInfo provider = CultureInfo.InvariantCulture;
DateTime dateTime;
string dateValue = null;
DateTimeStyles styles;
styles = DateTimeStyles.None;
if (DateTime.TryParse("01/01/2001 05:00", provider, styles, out dateTime))
{
Console.WriteLine(dateTime);
dateValue = dateTime.ToString();
Console.WriteLine(dateValue);
Console.ReadLine();
}
string dateSet = "01/01/2001 05:00";
Console.WriteLine(dateSet.Length);
if (dateSet.Length == 16)
{
dateSet = dateSet + ":00";
}
Console.ReadLine();
System.Console.WriteLine(DateTime.ParseExact(dateSet, "MM/dd/yyyy HH:mm:ss", provider));
Console.ReadLine();
}
catch (Exception Ex)
{
Console.WriteLine(Ex.Message.ToString());
Console.ReadLine();
}
}
}
}
Tip #2 – DataTable and LINQ Query Example
using System;using System.Globalization;
using System.Data;
namespace ExampleCode
{
public class ExampleCode
{
static void Main()
{
try
{
//DataTable TableA
DataTable dataTable1 = new DataTable();
//DataTable TableB
dataTable1.Columns.Add("Name", typeof(string));
dataTable1.Columns.Add("Age", typeof(int));
dataTable1.Columns.Add("Place", typeof(string));
dataTable1.Rows.Add("Raj", "21", "Chennai");
dataTable1.Rows.Add("Ram", "22", "Chennai");
dataTable1.Rows.Add("Rick", "24", "Mumbai");
dataTable1.Rows.Add("James", "15", "Delhi");
dataTable1.Rows.Add("Andy", "24", "Delhi");
var queryByCity = from myRow in
dataTable1.AsEnumerable()
where myRow.Field<string>("Name").Contains("Ra") &&myRow.Field<string>("Place") == "Chennai"
select myRow;
foreach (DataRow
dataValues in queryByCity)
{foreach (object dataValue in dataValues.ItemArray)
{
if (dataValue is int)
{
Console.WriteLine("Int: {0}", dataValue);
}
else if (dataValue is string)
{
Console.WriteLine("String: {0}", dataValue);
}
else if (dataValue is DateTime)
{
Console.WriteLine("DateTime: {0}", dataValue);
}
}
}
Console.ReadLine();
var queryByAge = from myRow in dataTable1.AsEnumerable()
where myRow.Field<string>("Place") == "Delhi" &&
myRow.Field<int>("Age") > 22
select myRow;
foreach (DataRow
dataValues in queryByAge)
{foreach (object dataValue in dataValues.ItemArray)
{
if (dataValue is int)
{
Console.WriteLine("Int: {0}", dataValue);
}
else if (dataValue is string)
{
Console.WriteLine("String: {0}", dataValue);
}
else if (dataValue is DateTime)
{
Console.WriteLine("DateTime: {0}", dataValue);
}
}
}
Console.ReadLine();
}
catch (Exception Ex)
{
Console.WriteLine(Ex.Message.ToString());
Console.ReadLine();
}
}
}
}
Tip #3 - Check for Entry in Dictionary
Unexceptional Dictionary Accesses in C#
Tip #4 - Log4J email on Error Sample Code link
Tip #5 - C# Working with Excel. Link1, Link2
Happy Learning!!!
No comments:
Post a Comment