Friday, October 19, 2007

parseInt - Javascript issue

What would be the result of the following javascript

var value = "08";
value = parseInt(value);
alert(value);

When we just go through the above script, we would expect the result as "8" should be alerted. But to all our surprise the result would be "0".

Let us analyze how this happened.

when ever the browser tries parseInt("08"), zero in front tells the browser that this is an octal (base 8) number and hence will be returned as "0".

This is the default behavior of the parseInt function, to read more about the function
click here.

So what we need to do in this case?

If we just check the intellisence result for the parseInt, it will be like


So it accepts another one argument called radix, and the definition of radix is as follows

"The radix parameter is used to specify which numeral system to be used, for example, a radix of 16 (hexadecimal) indicates that the number in the string should be parsed from a hexadecimal number to a decimal number."

And if the radix parameter is not supplied, browser assumes the following:
  • If the string begins with "0x", the radix is 16 (hexadecimal)
  • If the string begins with "0", the radix is 8 (octal). This feature is deprecated
  • If the string begins with any other value, the radix is 10 (decimal)

So in our case, browser assumed the radix as 8 and returned a value of "0". To solve this issue whenever we are calling the parseInt we should explicitly specify the radix value.

Thursday, October 18, 2007

XML PARSE ERROR: Missing end-tag - Error in Export to Excel

Last week i was debugging a strange problem which came in the export to excel option in one of our web page. We have a export method which, will accept the dataset and will loop through it and builds the XML schema for the excel.

When we were using this excel method to export some data, it went fine and the response rendered the Open or Save dialog box. But when we try to open the excel file it gave a error stating the following





Problem came up in following areas during load:

Table

This file cannot be opened because of errors. Errors are listed in: C:\Documents and Settings\UserId\Local Settings\Temporary Internet Files\Content.MSO\90797611.log




And when we open the specified 90797611.log file the message it had was,






XML PARSE ERROR: Missing end-tag
Error occurs at or below this element stack:
<ss:Workbook>
<ss:Worksheet>
<ss:Table>
<ss:Row>
<ss:Cell>
<ss:Data>
<ss:Font>
<ss:B>
<ss:I>








By looking in to this i was not able to get any thing, after doing some R&D I came to a conclusion that there were some problem with regarding "<ss:Font> <ss:B> <ss:I>".






Finally i decided to have a look at the dataset which is supplied to the export method, and to my surprise there was a field with the following data in it






<Font colour="green"><B><I>7358</B></I></Font>






As we are building a XML, the <Font colour="green"> got treated as a XML tag and it was expecting a corresponding end tag. This created the whole problem.



After removing <Font colour="green"><B><I> from the data, every thing worked fine.