Blog Stats
  • Posts - 17
  • Articles - 1
  • Comments - 28
  • Trackbacks - 0


Wednesday, April 22, 2015

C# Check if property exists before binding

I had a form view where I was binding two different objects to. These two objects were slightly different, and I didn't want to create a separate form view for both objects.

Here is what I did:

<%# Container.DataItem.GetType().GetProperty("Property1") != null ? DataBinder.Eval(Container.DataItem, "Property1") : DataBinder.Eval(Container.DataItem, "Property2")  %>

Tuesday, September 16, 2014

Loop through XML Element and set dynamic properties on object

Here is a handy method that accepts an XElement and an object. It loops through each attribute in the XML element and sets the matching property on the object:

        public static object SetProperties(object obj, XElement xelement)
            foreach (var attribute in xelement.Attributes()) //loop through attributes of element
                PropertyInfo propertyInfo = obj.GetType().GetProperty(attribute.Name.ToString()); //get property that matches attribute

                //make sure property exists
                if (propertyInfo != null)
                    if (propertyInfo.PropertyType == typeof(int) || propertyInfo.PropertyType == typeof(int?)) //int
                        propertyInfo.SetValue(obj, int.Parse(attribute.Value), null);
                    else if (propertyInfo.PropertyType == typeof(DateTime) || propertyInfo.PropertyType == typeof(DateTime?)) //datetime
                        propertyInfo.SetValue(obj, DateTime.Parse(attribute.Value), null);
                    else //string
                        propertyInfo.SetValue(obj, attribute.Value, null);

            return obj;

Wednesday, July 30, 2014

Sort table cells by child link

Often times sites will have horizontal menus created with tables. You can easily sort the cells by the containing links using jquery:

sortTD($("#topNav table tr"));

function sortTD(selector) {

    $(selector).children("td").sort(function (a, b) {

        var upA = $(a).children("a").text().toUpperCase();

        var upB = $(b).children("a").text().toUpperCase();

        return (upA < upB) ? -1 : (upA > upB) ? 1 : 0;



Friday, June 13, 2014

C# Replace Macros in Word Document .docx

One of my clients had users downloading a word document to edit and print out. Some of these fields in the Word Document can be pre-populated with data.

There is a dll called DocX available here:

Keep in mind, this only works with .docx, not .doc file formates

Sample code:

private void GetDocument()


            //Get parameters

            string FirstName = "Benjamin";

            string LastName = "Adler";

            string Username = "Badler";


            //Get template

            string TemplateFileName = @"C:\temp\test.docx";

            DocX template = DocX.Load(TemplateFileName);


            //Replace macros with parameters

            template.ReplaceText("%FirstName%", FirstName, false, System.Text.RegularExpressions.RegexOptions.Singleline, null, null, MatchFormattingOptions.ExactMatch);

            template.ReplaceText("%LastName%", LastName, false, System.Text.RegularExpressions.RegexOptions.Singleline, null, null, MatchFormattingOptions.ExactMatch);


            //Save with new file name

            string newFile = string.Format(@"C:\temp\test_{0}.docx", Username);



Monday, August 20, 2012

MSChart disappearing on postback

MSChart disappears on postback. The easiest fix is to simply enable viewstate, which is turned off by default:

<asp:Chart runat="server" ID="chartMyChart" EnableViewState="true" >

Friday, August 17, 2012

Getting MSChart to work on buildserver webserver with framework 3.5

MSCharting is a tool that helps you create all sorts of graphs in .NET. If you are using framework 3.5, you’ll need to install this:

 And this addon to make it display in toolbox and for intellisense:

If you are using framework 3.5, you’ll need to either install the charting controls on each server that you are working on, or you can include the dlls in your project instead and reference these dlls as if they were coming from a 3rd party.

When you install the Charting Controls, they are located here C:\Program Files\Microsoft Chart Controls. Now you can reference them in your project if needed.


Thursday, July 12, 2012

Report Builder 2.0 - Requested value 'Integrated' was not found.


I was trying to open a .rdl file but could not due to an error:

Requested value 'Integrated' was not found.

I could open the .rdl in Report Builder 3.0, but I couldn’t upload it to the reporting server because it was the wrong schema. I also couldn’t open the .rdl that was saved in 3.0 in 2.0 for the same reason.

I didn’t want to go through the time of installing Business Intelligence Studio. I discovered I didn’t have to:

1.       Open .rdl in notepad to edit the XML

2.       Replace <rd:SecurityType>Integrated</rd:SecurityType>

with <rd:SecurityType>Windows</rd:SecurityType>

3.       Save and open the .rdl in Report Builder 2.0!






Friday, October 21, 2011

Ajaxtoolkit HTMLEditor color picker position not visible off screen

I was working with the built in ajaxtooljit HTMLEditor and for some odd reason, the color picker was showing off the screen. I noticed that the "TextEditorDivContainer" class of the control had position absolute.

To solve this problem, I had to wrap the editor in a div with a class named "EditorWrapper" and change the position of the "TextEditorDivContainer" class to static.


.EditorWrapper .TextEditorDivContainer {     position: static !important; }


Thursday, September 22, 2011

jwplayer file not found or access denied checklist

A common problem when using jwplayer is "file not found or access denied".

Here are a few things to check for:

1.       If you are using mp4, be sure to have the MIME type added in IIS. The MIME type for .mp4 is video/mp4. The video will work in a test html page without a MIME type, but you’ll need to add one when working with

2.       Be sure the file location is correct and that the file exist and is named correctly. You may want to try using an absolute web path name inside the javascript for testing purposes.

3.       Check your relative file path. Some versions of the player (v5.3), a relative path must be relative to the SWF file, other versions, the relative path must be relative to the html which the SWF is embedded (v5.4) . See here

4.       Be sure you are using forward slashes instead of backslashes in the file name

5.       View the source of the javascript to confirm it's correct.

Wednesday, September 14, 2011

The tail of the log for the database "%ls" has not been backed up.

I was working locally and trying to restore a database backup from the server for development purposes. I was receiving this error when I tried to restore and overwrite an existing database:

The tail of the log for the database "%ls" has not been backed up. Use BACKUP LOG WITH NORECOVERY to back up the log if it contains work that you do not want to lose. Use the WITH REPLACE or WITH STOPAT clause of the RESTORE statement to just overwrite the contents of the log.

An easy fix is to click on the Options page and click 'Overwrite the existing database (WITH REPLACE).

More info here:





Copyright © BenAdler