Friday, December 23, 2011

Appending elements while iterating an XMLNodeList

In a unit test, I had created a populated XMLNodeList and wanted to make sure that the elements were added in a certain spot within the (same parent) XMLDocument after the code under test was finished. I don't remember many of the minute details, but the code would iterate the XMLNodeList and copy the node to the document. The iteration kept failing, however.

After stepping through the code in debug mode, I found that the original XMLNodeList would get emptied out while iterating. If you want to keep the XMLNodeList intact, you need to clone the current node and push the clone into the document.

Wednesday, December 21, 2011

ASP.NET UpdatePanel and PageRequestManagerParserErrorException

I have a page that contains an UpdatePanel that gets updated every 2 seconds to poll for information from the server. With a certain combination of the application running, the UpdatePanel would not refresh in Internet Explorer, but worked fine in Firefox. I finally tracked it down to the UpdatePanel throwing a PageRequestManagerParserErrorException when the data was coming back from the server. There is plenty of information out there about common reasons why this exception is thrown. However, none of those suggestions are valid for my particular need, which is why I can post about it here.

The data coming back from the server included non-displayable characters. Firefox handled this gracefully by displaying a little glyph in the place of the non-displayable characters. Internet Explorer choked on the non-displayable characters. Since I have some control over the data that gets sent to the view, I intercept the non-displayable characters and change them to ‘?’. Now both Internet Explorer are handling the refreshing correctly.

Thursday, November 17, 2011

XML Elements From Different Documents

A coworker and I were working on a feature that required manipulating XML through the .NET XML DOM. Per our standard operating procedure, we were using test-driven development. We were updating an existing piece of code to do something different so we were working within an existing design that made use of direct calls to the XMLDocument object.

We went through a few iterations of red, green, refactor and finally arrived at an implementation that should have worked. Our test had an instance of XMLDocument that was creating nodes that we were sending into the code. But the code had its own XMLDocument that it was using to build the final XML structure. We kept getting an error with the message "The node to be inserted is from a different document context."

After searching around, we eventually found that the XMLDocument is very selfish in which nodes it allows to be added. If you attempt to add a node within an instance of XMLDocument that was not the originating instance of that node, you will receive the same error.

The solution in our case was to pull the XMLDocument instance creation out of the piece of code under test and pass it in as a modifiable parameter.

Thursday, July 7, 2011

Redirecting to Different Controllers in ASP.NET MVC

Last year, we started using ASP.NET MVC at work and are still learning its intricacies. About a month ago, I needed to put in a button that redirected to another part of the application. I have sanitized the details because they aren't germane. The button was located on a page controlled by a different controller than the page I wanted to redirect to. I tried the following 3 incorrect snippets before seeing the error, which is corrected in the last snippet.

<HttpPost()> _
<ActionName("Index")> _
<HandlesButton("redirectingaction")> _
Public Sub RedirectToOtherController()
End Sub
<HttpPost()> _
<ActionName("Index")> _
<HandlesButton("redirectingaction")> _
Public Sub RedirectToOtherController()
End Sub
<HttpPost()> _
<ActionName("Index")> _
<HandlesButton("redirectingaction")> _
Public Sub RedirectToOtherController()
  RedirectToAction("NewAction", "OtherController")
End Sub

Can you spot the difference? Some of our MVC actions perform operations on the server with no need for a return value. But the redirect needs a return value to switch views. So I needed to have a function with a return statement.

<HttpPost()> _
<ActionName("Index")> _
<HandlesButton("redirectingaction")> _
Public Function RedirectToOtherController() As ActionResult
  Return RedirectToAction("NewAction", "OtherController")
End Function

Seagate FreeAgent Error 2013

I have a Seagate FreeAgent external drive to help with backups.

About 2 weeks ago, the nightly backup started failing. It was always one particular file that couldn't get copied and the error code was 2013.

A number of sites from the Google indicated that it was a file permission issue on both the backup drive and the source drive. I made sure that the file was not marked as read-only, but the backup still kept failing.

I finally stumbled upon this post from the Seagate website itself.

Per the article, I deleted the offending file from the backup drive and restarted the backup. It is now working again.

Wednesday, June 29, 2011

PHPUnit and Working Directories

I'm using the PHPUnit framework for unit testing some code I'm writing for a couple of websites. To avoid figuring out the seemingly convoluted API for building a suite of tests, I had to rearrange my directory structure. All of my test classes are in a Tests directory that is a child of the directory where the working code is located. I also had to split out each test class into its own separate file (similar to Java).

In moving the tests to a subdirectory, I had to change my require_once statements to point to the parent directory via a relative path. At least, I thought I did.

It turns out that require_once operates on the current working directory. I was using the following command:
  C:\Source\> phpunit Tests
The current working directory was C:\Source, so when the require_once attempted to resolve the path, it was looking in C:\.

There are two solutions:
  1. Remove the relative path markers
  2. Run phpunit from within the Tests directory

I chose option 2.

Monday, April 4, 2011

WiFi Setup Issues

For most of the month of March, we were in Brazil visiting friends who are serving as missionaries. Our purpose was not to just visit, but to also help out. As I have some skills with computers, I helped with a few computer issues. I also worked on the wireless network. I brought some access points and routers with me and worked on expanding the coverage.

It was fairly easy to get the new access points and router set up and plugged in, so I thought. Soon after I "finished", I started hearing about intermittent Internet access. Everybody was having trouble. So, I unplugged the new ones and went to the main router to see what was going on.

There was already a router on the network and I added one. The new router had DHCP enabled and was going through the original one for Internet access, but was on a different subnet

Some of the veterans out there may already have an idea about the issue.

The new router was getting an IP address on a different subnet than it was assigning IP addresses on. All devices attached to the new router were getting conflicting IP addresses.

I had to turn off DHCP on the new router. I also set the static IP addresses for the access points and new router to be on the subnet of the original router.

I learned more about wireless networking than I really wanted to, but now I have some more questions in my troubleshooting script.

Monday, March 7, 2011

Mercurial Credentials

We are switching to Mercurial from CVS at work to hopefully reduce some of the inefficiencies we encounter when pushing new features into production. My initial impression was not favorable because I had to enter my credentials for every single operation. "I entered my username and password 10 seconds ago. You want them again?"

My thoughts then turned to Google. Surely I'm not the only one who is annoyed by this. Sure enough, I wasn't. Before you think I'm violating first principles, bear with me.

The first way I found was to store my username and password in the mercurial settings the clear. Storing passwords in the clear is unacceptable.

The next suggestion was to use the keyring extension that came in the TortoiseHG install. The keyring extension encrypts passwords before storing them, so you can enter once and not worry about it.

The tutorial for configuring the keyring extension was easy enough to follow. However, we ran into a problem with SSL on our main repository server. Once we put the certificate fingerprint into the mercurial.ini file, we were able to clone from the main server into our local sandboxes.
default = https://<hostname>/path/to/Repository

<hostname> = <certificate>
To get the value for the certificate:
  1. Navigate to the path specified for the repository in FireFox.
  2. Click on the icon to the right of the URL once the page has finished loading.
  3. Click 'More Information' button.
  4. Click the 'Security' icon in the dialog that appears.
  5. Click the 'View Certificate' button.
  6. Copy the SHA1 Fingerprint value into your mercurial.ini file.

Monday, February 28, 2011

Unit Testing Language Features

I was cleaning up some code I was working on and found a variable that was only used to hold the results of a function that returned a list for enumeration. I could make the code a little more readable by moving the function call directly into the loop. My only question was whether the function would be called each iteration.

We use NUnit at work for test-driven development and automated regression tests. If it works for testing scenarios in the code that we write, why not for language features also? Here is the test I came up with.
<Test(), Explicit()> Public Sub ForEachTest()
  Dim numRuns As Integer = 0

  For Each s As String In TestForEach(numRuns)

  Assert.AreEqual(1, numRuns)
End Sub

Private Function TestForEach(ByRef numberOfRuns As Integer) As IList(Of String)
  numberOfRuns += 1

  Dim result As New List(Of String)
  result.Add("Test 1")
  result.Add("Test 2")
  result.Add("Test 3")

  Return result
End Function
If the iterator calls the function multiple times then the test would fail. I marked the test to be Explicit() so that it would not run with our automated build in the case I accidentally checked it in.

Sunday, February 27, 2011

Intent: Part 2

I use a number of different programming languages and technologies depending on what I am currently working on. Some projects have requirements that lend themselves to one technology or language over another. You will be able to determine which language or technology I'm working with based on what the post contains in it. I will post code samples in the language I am using at the time I have a blog post idea.

Friday, February 25, 2011


Sometimes 140 characters just isn't enough space. Sometimes I want to post about things that don't really fit with my family blog. This is a place for such topics.

I am a software engineer by day. I am paid to solve problems on computers. A lot of problems that I run into have already been solved and Google helps me to not reinvent the wheel. Nevertheless, I do occasionally run into questions that do not have a readily available answer (namely the first few pages of a Google search).

My intent with this blog is to post primarily about computer programming topics. I reserve the right to occasionally differ. I also do not commit to a regular posting schedule.