And for My Next Trick...

When I arrived in the bay area about three years ago, I felt like somewhat of a pariah given that I had been most recently working with Microsoft technologies and was trying to get a job amongst LAMP developers in the most competitive place in the world to do so. I fell back on my previous Perl experience and found a sweet gig at Yahoo in what essentially is the dev tools part of the organization.

A few years later, now that I've been able to come up for some air and take a look around, I've noticed that the world has gone the way of RoR and Python. I was telling a friend recently, "it sure is hard to find web dev positions for Perl developers" and he responded (because he's looking to fill some reqs,) "it sure is hard to find Perl developers for our web dev positions."

So, which of RoR and Python to learn first? I'm a big believer in learning by doing, so now I just need to sit down and develop a service in one of the two. Python, bring it on.

Trouble with OnSubmit() and CompareValidator

To do some validation on two dates (an end date and a start date where the end date shouldn't be earlier than the start date,) I tried making use of the CompareValidator. After much frustration, I finally got rid of it and coded the JavaScript for the validation myself. I was making use of a CompareValidator control in a webform along with a hyperlink-as-submit-button ala this tutorial. When I tested the hyperlink, I got this:
'event' is null or not an object
When I tried this...
alert(document.theForm.onsubmit);
I got this in the popup...
function anonymous() {
          if(!ValdiatorOnSubmit()) 
               return false;
  }
...which seemed to indicate that the onsubmit event is hooked into the .NET web validation javascript via a pointer somewhere. What to do? How about including an invisbile submit button to force the form submission (thank you W3Schools!) ala:
document.getElementById('InivisibleSubmitButton').click();
The problem I ran into was that I couldn't test whether the technique returned true (validation OK) or false (try again) which was necessary before submitting the values to commit to the database. In the end, I wound up including my own JavaScript to validate the dates. No post backs, just simple validation client-side.

XMLElement for Suppressing a Node Element

As I was recently serializing objects into XML, I had the need to suppress a few of the elements in the ouput. After looking around, I finally found "XMLElement" and some examples which I reworked in VB .NET. To start with, I have a base class with a method called GenerateXML which each inheriting class overloads. In the method, I use an XmlSerializer to generate the XML and everything was working quite well, until the day before yesterday. The main problem was in a particular subclass that has a property with type DateTime. As all of you know (as do I now,) anytime you try to serialize such an object, you get this in the XML for the property:
<MyDateProperty>0001-01-01T00:00:00.0000000+01:00</MyDateProperty>
When SQL Server tried to update the appropriate table column with the value, I got:
The conversion of a char data type to a datetime data type resulted 
in an out-of-range datetime value.
The statement has been terminated.
I thought the DateTime datatype would be consistently nullable between the .NET framework and SQL Server. Guess I was wrong. The trick became to figure out how to supress the MyDateProperty element in the serialized XML. What to do:
  • Develop a stylesheet to transform the xml before passing it to the stored procedure?
  • Create a new property with type string in the class and then use hidden inputs on the web form to set and get values?
  • Set the data type of the property to string and then marshal the string into a date and vice-verse where necessary?
So many ways to skin a cat. Then I started thinking "there has to be a way to control serialization of the class in the XMLSerializer." I found the XMLElement attribute along with a really good thread applicable to my case here. There were only two differences in my case:
  • I'm using VB .NET
  • I needed to figure a way out of how to do it design-time in the class and not when the class gets serialized by the XMLSerializer
After a good deal of tweaking, I finally got the xxxxSpecified idea (see above) to work inside my subclass. Here's the (VB .NET):
<XmlElement(ElementName:="MyDateProperty", DataType:="date")> _
                                  Property MyDateProperty() As DateTime 
               Get 
                    Return _MyDateProperty 
               End Get 
               Set(ByVal Value As DateTime) 
                     _MyDateProperty = Value 
               End Set 
         End Property  
<XmlIgnore()> Property MyDatePropertySpecified() As Boolean 
               Get 
                     Return _ShowIgnoredElement 
               End Get 
               Set(ByVal Value As Boolean) 
                     _ShowIgnoredElement = Value 
               End Set 
         End Property
All that's left is to set the MyDatePropertySpecified property in a constructor of the subclass. Here's an example:
' Author:  Anthony Jay Bull
' Date:  2006-11-21
' In:  All the params off of the query string as provided 
'       by a NameValueCollection object
' Notes:  The MyDateWebControl is providing the date in this case
'       in the format of "YYYY-DD-MM"

Public Sub New(ByVal formParams As System.Collections.Specialized.NameValueCollection)

           Dim arrPickedDate() As String
           arrPickedDate =_
                      HttpUtility.HtmlEncode(formParams.Item("MyDateWebControl")).Split("-")
           If arrPickedDate.Length = 3 Then
                      Me.MyDateProperty = New DateTime(arrPickedDate(0), _ 
                                             arrPickedDate(2), arrPickedDate(1))
                      Me.MyDatePropertySpecified = True
           Else
                      Me.MyDatePropertySpecified = False
           End If
End Sub
Now, instead of this:
<SomeRootElement>
     <SomeSiblingOfMyDate>Some string value</SomeSiblingOfMyDate>
     <MyDateProperty>0001-01-01T00:00:00.0000000+01:00</MyDateProperty>
</SomeRootElement>
...or even this:
<SomeRootElement>
     <SomeSiblingOfMyDate>Some string value</SomeSiblingOfMyDate>
     <MyDateProperty/>
</SomeRootElement>
I now get this...
<SomeRootElement>
     <SomeSiblingOfMyDate>Some string value</SomeSiblingOfMyDate>
</SomeRootElement>

When I try to use an stylesheet extension, why do I get HRESULT: 0x80131418 ?

I was trying to make use of Jeni Tennison's extension for evaluating a string query (found here among other places) when I kept on getting "HRESULT: 0x80131418." Even when I stepped through the code I couldn't find any more information about the exception so I Googled it to see what I could find. Long story short, it appears to be a security policy exception. Ah... that might make sense. We're compiling the stylesheet script at run time so the managed code environment looks like it's doing its job by enforcing whatever security policy's in place. Ok, what's the workaround? I tool a look at the most appropriate MS documentation I could find. It says: "Support for embedded scripts is an optional XSLT setting on the XslCompiledTransform class." "Script support is disabled by default" "XSLT scripting should be enabled only if [...] you are working in a fully trusted environment." Ah, clues... "XslCompiledTransform" is only defined in .NET 2.0. The transformation is happening within a .NET 1.1 command-line application I'm running on my machine. Hmmm... To solve the problem, I just changed the local security settings with caspol.exe and the stylesheet script compiles and runs.

SOAPAction?

Can anyone really tell me what the SOAPAction header means or is good for?   I was getting a faultstring saying "Unable to hanlde request without a valid action parameter" and didn't know what to make of it.  I dug around (read:  Googled for it) and found an RFC or two (and you know I love reading those...) but couldn't find a solid answer.  My solution?  I just thought to look in the WSDL and low and behold, there was a SOAPAction key-value pair which I wound up using in my AJAX call and it worked.  Go figure.

Custom Controls for ASP .NET

Today I was doing an XML transformation (with XSLT) in a Page_Load when I realized that the timing was off. I realized that the output was being sent too early to the response stream and I thought "I've seen this before... How do I fix this?" Thank goodness for Custom Controls, or, I should rather say, being able to override the Render method. In looking around about how exactly to do that, I discoverd this tutorial on Developing a Simple Custom Control.