Category: Technology

Enterprise Library Sometimes Chops XML into 2033 Characters

This morning I began what I thought would be an uneventful task in .NET: connect to one database, download as XML some rows from a table that have changed since a given time, then pass that XML to a stored procedure in another database so that the changes can be merged. Just two tables: one relatively small, the other just a 2-column relationship table for the first table. Here’s a contrived snippet to serve the same purpose:

The SQL:

CREATE PROCEDURE dbo.GetXmlValue

AS

SELECT TOP 100
*
FROM sys.all_columns
FOR XML AUTO, ROOT('Root')

The Code:

Database db = GetDatabase();

DataSet set;

using (DbCommand cmd = db.GetStoredProcCommand("dbo.GetXmlValue"))
{
set = db.ExecuteDataSet(cmd);
}

In my application, it was this second table that tripped me a bit. The first only had a few changed rows, so the XML was only 500-1000 characters. The second table generated a much longer XML string – 3864 characters long. My initial plan was to return two tables, each 1 column and 1 row. When using Database.ExecuteDataSet to fetch the results, however, the second table yielded two rows. I backtracked, second guessed myself, and even grabbed a couple of guys in the hall to double check my code. No one could explain it. It seemed odd that this hasn’t come up before for someone here. I did a bit of Googling and only found a couple of results. This Microsoft Knowledgebase Article just says it’s “by design” and to use SQLCommand.ExecuteXmlReader.

The next question that came up was “Well, what does it do if I return a long XML value among other values in the same row?” I tested this out and everything worked as expected. All of the XML was in a single column value as it should be.

I still can’t exactly explain it. ExecuteScalar has a character limit of 2033 characters. I suspect that because each table in my result set is just a single column value, something somewhere is relying on ExecuteScalar.

My solution? In my stored procedure I just stored each of my table results as an XML typed variable, then returned the two together in the same row.

GTIN 12 UPC Check Digit Calculation in SQL Server 2008

I recently needed to generate the check digit for a GTIN-12 UPC code in SQL. The calculation method for the check digit varies depending on what kind of barcode you’re implementing, so I popped on over to Google to get the necessary algorithm. Naturally, and regardless of simplicity, I checked for any available code snippets. The only I found weren’t GTIN-12, and were in C or C++. So for anyone looking to do it (or something similar) in T-SQL, here’s a scalar function to do just that.

CREATE FUNCTION dbo.CalculateBarcodeGTIN12CheckDigit(@input CHAR(11))
RETURNS INT
AS
BEGIN
DECLARE @evenDigitSum INT = 0
,@oddDigitSum INT = 0
,@i TINYINT = 0
,@result INT;

-- check if given Barcode is Numeric , if not return error status -1
IF(ISNUMERIC(@input) = 0
OR LEN(RTRIM(LTRIM(@input))) != 11)
RETURN -1

-- start the compute BarCode checksum algorithm
SET @i = 1
WHILE (@i <= 11)
BEGIN
--Add odd and even digits separately;
IF((@i % 2) = 0)
SET @evenDigitSum += CONVERT(TINYINT, SUBSTRING(@input,@i,1))
ELSE
SET @oddDigitSum += CONVERT(TINYINT, SUBSTRING(@input,@i,1))
SET @i += 1
END

--As per: http://en.wikipedia.org/wiki/Universal_Product_Code
--Multiply odd sum by 3, add to even sum, and mod 10.
SET @result = ((@oddDigitSum * 3) + @evenDigitSum) % 10;
IF(@result = 0)
RETURN 0
ELSE
RETURN 10 - @result;

RETURN -1
END
GO

Scalar functions are killer, but I’ll only ever be using it for a single record.

360 Panorama – Occip.It View After Saving

Per the comment thread on Occipital’s blog:

This took me a while to find. As I’m in Berlin right now, my phone doesn’t work. I’ve pulled the SIM and I am just using it like an iPod touch. This has been a frustrating experience with regard to 360 Panorama because I rarely have an internet connection at the time that I take the panorama. All I can do is “Save”, and not having it in their nifty “Immersively stunning way” is a bit of a letdown.

The good news is twofold: They’re going to address it in an update AND there’s a way to manually recreate them with your saved image. First, upload them to www.yfrog.com. Luckily, they allow batch uploads. So if you’re like me and took 52 of them, it’s not too much trouble to upload them all.

Then the tedious part: making a list of all of the URLs created by the newly uploaded images. Once you make a list, just replace “yfrog.com/” with  ”occip.it/py” and voila! you have an Occipital magic view of your 360 panorama after saving it.

If anyone has any quick way of creating a list of all of your yfrog images, I’d love to know.

It’s not an elegant solution, but it should work until they update the app.

UPDATE: See comment for some of mine from my trip.

UPDATE 2: Checked for an app store update the very next day and it looks like they’ve addressed the problem.

Further Proof That Any “Win an Apple XYZ Product” Is A Scam

I have this theory that contests via which you win a free Apple product are all a myth. Until I win something first hand, I will maintain this theory. This morning I had an email from Sci-Port letting me know they’re having a contest to win an Apple iPad:

WTFScipPortiPad

 

Anyone else notice something a bit… um, fabricated about this? Do they know something about the next iPad that we don’t?