studio NETSOULS

Applying Web To Your Business

I am always searching the internet for the DateTime formats, so i thought i will post it in my blog so that i don’t need to keep googling for it.

Specifier String Result Output
d dd-MM-yyyy 03-01-2009
D dd MMMM yyyy 03 January 2009
f dd MMMM yyyy HH:mm:ss 03 January 2009 17:31:14
g MM/dd/yyyy HH:mm 03-01-2009 17:31:14
m MMMM dd January 03
r ddd, dd MMM yyyy HH':'mm':'ss 'GMT' Sat, 03 Jan 2009 17:31:14 GMT
s yyyy'-'MM'-'dd'T'HH':'mm':'ss 2009-01-03T17:31:14
t HH:mm:ss 17:31:14
u yyyy'-'MM'-'dd HH':'mm':'ss'Z' 2009-01-03 17:31:14Z
U dd MMMM yyyy HH:mm:ss 03 January 2009 12:01:14
y MMMM, yyyy January, 2009
o yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK 2009-01-03T17:31:14.0303453+05:30
DateTime todayDate = DateTime.Now;
string dateToStringInFormat = todayDate.ToString(formatSpecifier, 
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat)
  • Currently 0 /5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

A lot of times in the applications we require to pass comma separated values to the stored procedures, these values are contained in a custom object IList or IList<string> type. 

/// <summary>
/// Converts a list item property name value to comma seperated values
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="Items"></param>
/// <param name="PropertyName"></param>
/// <returns></returns>
public static string ConvertTo<T>(IList<T> Items, string PropertyName) 
{
    StringBuilder builder = new StringBuilder();
    Type entityType = typeof(T);
 
    // iterate through the property
    foreach (T Item in Items)
    {
        if (string.IsNullOrEmpty(PropertyName))
            builder.Append(Item.ToString()).Append(",");
        else 
            // get the value of the property name passed
            builder.Append(
                entityType.GetProperty(PropertyName).GetValue(Item, null).ToString()
            ).Append(",");
    }
 
    return builder.ToString().TrimEnd(new char[] { ',' });
}
  • Currently 2 /5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

If you've worked on creating a Data Access Layer you know how frustrating it can be to determine the type of data your DAL will be handling. For that reason, creating a converter that works with generics or vica-versa can be the long term answer.

Alright, so for our applications we have a couple of forms that use the DataGridView control to display tabular data (kinda of a common scenario for many business applications). The data is returned from our services as an IList<T> in which we can just then bind directly to the grid by using the grid's DataSource property.  Pretty easy task...unless when it comes to data manipulations.

So, what we decided to was take the easy way out and that is convert our IList<T> to a DataTable object with the correct schema (for the primitive types) that our contained objects have.  Here's what we came up with:More...

  • Currently 4 /5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

I lot of times in our Business Object Model we use enums, these enum property could be a string, integer or a short value. While retrieving records from the database we need to set the property in the BOM to that of the enum value.

Here is one of those methods that you can never find when you're looking for it

   1: // suppose we have a enum
   2: enum PublishingStatus : short
   3: {
   4:     Draft = 1,
   5:     PendingApproval = 2,
   6:     Active = 3,
   7:     Archived = 4,
   8:     PendingDeletion = 5
   9: }
  10:  
  11: //set a default value
  12: PublishingStatusEnum CurrentPublishingStatus = PublishingStatusEnum.Draft;
  13: if (Enum.IsDefined(typeof(PublishingStatusEnum), Helper.GetShort(Reader["CurrentPublishingStatusId"])))
  14:     CurrentPublishingStatus = (PublishingStatusEnum)Enum.Parse(typeof(PublishingStatusEnum), Reader["CurrentPublishingStatusId"].ToString());
  15:  
  16: // suppose we have a string enum
  17: enum SiteColors
  18: {
  19:     Red,
  20:     Blue,
  21:     Orange
  22: }
  23:  
  24: //set a default value
  25: SiteColors SelectedColor = SiteColors.Red;
  26: if (Enum.IsDefined(typeof(SiteColors), Reader["SiteColor"].ToString()))
  27:     SelectedColor = (SiteColors)Enum.Parse(typeof(SiteColors), Reader["SiteColor"].ToString(),true);
  28: // we passed the last variable as true of ignoreCase
  • Currently 0 /5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Lets you have two datatables having two same columns. This code is useful to merge the columns.

   1: public static DataTable MergeTables(DataTable dtFirst, DataTable dtSecond, string CommonColumn)
   2:    {
   3:        DataTable dtResults = dtFirst.Clone();
   4:        int count = 0;
   5:        for (int i = 0; i < dtSecond.Columns.Count; i++)
   6:        {
   7:            if (!dtFirst.Columns.Contains(dtSecond.Columns[i].ColumnName))
   8:            {
   9:                dtResults.Columns.Add(dtSecond.Columns[i].ColumnName, dtSecond.Columns[i].DataType);  
  10:                count++;
  11:            }
  12:        }
  13:  
  14:        DataColumn[] columns = new DataColumn[count];
  15:        int j = 0;
  16:        for (int i = 0; i < dtSecond.Columns.Count; i++)
  17:        {
  18:            if (!dtFirst.Columns.Contains(dtSecond.Columns[i].ColumnName))
  19:            {
  20:                columns[j++] = new DataColumn(dtSecond.Columns[i].ColumnName, dtSecond.Columns[i].DataType);
  21:            }
  22:        }
  23:  
  24:        dtResults.BeginLoadData();
  25:        foreach (DataRow dr in dtFirst.Rows)
  26:        {
  27:            dtResults.Rows.Add(dr.ItemArray);
  28:        }
  29:        foreach (DataRow dr in dtSecond.Rows)
  30:        {
  31:            foreach (DataRow dr1 in dtResults.Rows)
  32:            {
  33:                if (dr1[CommonColumn].ToString().Equals(dr[CommonColumn].ToString()))
  34:                {
  35:                    foreach (DataColumn c in columns)
  36:                    {
  37:                        dr1[c.ColumnName] = dr[c.ColumnName];
  38:                    }
  39:                }
  40:            }
  41:        }
  42:        dtResults.EndLoadData();
  43:        return dtResults;
  44:    }
  • Currently 0 /5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

About Us

studio NETSOULS is a complete IT services company, offering strategy, design, development and implementation of the total solution for your web and IT initiatives. The solutions we provide, enables businesses to leverage leading edge technology to gain sustainable competitive advantages in today's marketplace.

We specialize in designing, developing and deploying the next generation of IT solutions including e-business solutions Read more...

Tags

This will be shown to users with no Flash or Javascript.

Contact Us

My status

Quote of the Day

Men are disturbed, not by the things that happen, but by their opinion of the things that happen

- Epictetus

NutritionVista

www.NutritionVista.com

Archives


Advertisements


Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Log in