Thursday, July 18, 2013

Copy Property Values

If you have 2 objects that do not inherit from each other but have similar properties, here is a function you can use to copy property values from one object to the other:

    public static void CopyIdenticalProperties (this object source, object dest)
        {
            var plist = from prop in source.GetType().GetProperties()
                        where prop.CanRead && prop.CanWrite
                        select prop;

            foreach (PropertyInfo prop in plist)
            {
                var destProp = dest.GetType().GetProperty(prop.Name);              
                if (destProp != null)
                {
                    destProp.SetValue(dest, prop.GetValue(source, null), null);
                }
            }
        }

Wednesday, June 12, 2013

Format Phone Number

This is how you could format a phone number using a period as a separator:

String.Format("{0:###\\.###\\.####}", 1234567890);

The resulting string will look as follows: 123.456.7890.

Wednesday, November 28, 2012

Update WCF Service Site References

If you have a problem updating dll files in the bin folder of your Web Site deployment project, the following method may work: delete all dll files in your bin folder, and then add a reference to you Service project. All the other required dll files will be added automatically.

Thursday, May 31, 2012

Regex for Month Number

This is the regular expressions pattern I use to test whether string characters represent a month number (from 01 to 12):

(0[1-9]|1[0-2])

The expression part inside the brackets is broken into 2 possible parts divided by a vertical bar (or pipe symbol): 0[1-9] or 1[0-2].

If a string matches any of the 2 parts it is true.

The first one says that the 2-character string has to start with 0 and end with any number in the range from 1 to 9, e.g. 01, 02, 09

The second part matches any 2-character string that starts with 1 and ends with 0, 1 or 2, i.e. 10, 11, or 12

Monday, March 12, 2012

Delete Contents of IsolatedStorageFile

The easiest way to delete contents of an IsolatedStorageFile is to open it in the Truncate mode:

IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForAssembly();

IsolatedStorageFileStream isoFile = new IsolatedStorageFileStream(myFileName, FileMode.Truncate, isoStore);

According to the MSDN , if a file is opened in the Truncate mode its size should be 0 bytes.

Friday, December 2, 2011

Delete ScriptManager History

Well, I have not found a way to clear ScriptManager history proper, but the workaround  is to use the AddHistoryPoint method and set the value of your property to "0":

ScriptManager1.AddHistoryPoint("myKey", "0");


Tuesday, October 25, 2011

Check if GridView Column Exists

If you have a DataRowView you can use the following extension method to check if a GridView column exists:

public static bool ColumnExists( this  DataRowView rowData,  string  fldToCheck)
{

return rowData.Row.Table.Columns.Contains(fldToCheck);
}

Normally, you would use it on RowDataBound event, e.g:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

DataRowView rowData = e.Row.DataItem as DataRowView;
string  fldToCheck = "MyFieldName";
if (e.Row.RowType == DataControlRowType.DataRow)
{
      if (rowData.ColumnExists(fldToCheck) ...


Related posts:
Find GridView Column Index
Set DataRow Values and Other ASP.NET GridView Tips