Archive for the '.NET' Category

07
Dec
09

BLOBs – The AIG of Data Integrity

OK – so there is no one-size-fits-all way to define the use of BLOBs in your database.  But this crazy bandwagon of putting file storage into BLOBs really has me baffled.  I think it is…well…down right stupid.  Why the metaphor with AIG – because people are treating BLOBs like they represent an additional layer of “insurance” against catastrophic failure… they pretend that if they use BLOBs, their solution must fall into the “Too big to fail” category and will qualify for a stimulus package. Hog Dasz.  That’s a technical term created from washing hogs using ice cream.  Using BLOBs, to me, is like storing your household pets in a vacuum cleaner.

When companies like Microsoft (e.g. SharePoint) and Metastorm embrace BLOBs as they have – they send out waves of marketing that indirectly imply that, since their solutions are the bestest everer, BLOBs must be the BEST way to store files; However, when comparing storage of files on a file system (or file share), vs. storing them in a database as Binary Large Objects (BLOBs),  my vote is against BLOBs.  IMHO, filling a database with BLOBs:

  • Causes unnecessary bloat on the database, making it harder to manage
  • Causes unnecessary processing, as the files must be marshaled into and out of the database
  • Introduces risk because once the files are in the database, the only way to get them back out is through the vendor application
    • This marries the organization to the vendor
    • If the database server goes down, so do the files
      • This same argument could be made for file servers – if a file server goes down then the files cannot be accessed.  Read on for my counter to that opinion.
  • Introduces undue complexity
    • If a file server goes down, it can readily be restored from backups without requiring the vendor product be installed and configured, and without the installation and configuration of SQL Server or another database.
    • File systems have the ability to work with RAID and SAN architectures directly for redundancy – adding them to BLOBs buys nothing but the above hassles
    • Providing additional file storage is comparatively easy
    • In a virtualized environment the files are a a couple layers of abstraction closer to the hardware – that is NOT a bad thing!

Maybe someone will have a rationale in favor of BLOBs, maybe there is a time when even I will leverage a BLOB intentionally.  But I am hard-pressed – my current perception says, if it is a BLOB, treat it like a file and store the pointer in the database.  Yes, the pointers must be maintained.  Yes, the permissions on the file system should stop people from willy-nilly moving files around.  Yes, perhaps there is some loose coupling with the software that manages those files, but in a pinch…you HAVE your files…and NOBODY CAN TAKE THAT AWAY FROM YOU! (sniffle, hug)

Stay Beautiful.  Stay BLOB Free.

06
Dec
09

SQL Server 2008: Adding an Identity to Existing Column

No. You are probably not.  Here is a SHORT ANSWER (just to show the concept) for putting an IDENTITY on an existing column, based on what I’ve seen:

  • Use All Tasks -> Generate Scripts to script the schema of the table(s) you want to alter.  Script it to an SSMS query window.
  • Go through the script and put a “2” on the end of the table name(s), and  add “IDENTITY(1,1)” to the int column you want to make an identity:
    • e.g. CREATE TABLE MyTable2 (
      MyIDColumnName int IDENTITY(1,1)
  • Run the script (I do this one table at a time) – this creates a duplicate table that HAS the identity column you want.
  • For each table you will need to run a script like this:
    • SET IDENTITY_INSERT MyTable2 ON
      INSERT INTO MyTable2 ([you must list out all the columns...sorry, SSMS requires this])
      SELECT [yes you must list all the columns again, in the SAME order] FROM MyTable
      SET IDENTITY_INSERT MyTable2 OFF
    • Note: Notice the select is from your ORIGINAL table – no “2″ on the table name, because you are inserting the data from the table that doesn’t have the identity column to the one that does
  • Now you can drop your original tables, and then rename the new tables by getting rid of the “2″ on the name.

NARRATIVE

Now, I hope someone finds a better way – that would be great.  I think this approach, if it IS the answer, is lame.  We are in the year 2010.  2010!!?? And I can’t get a column to populate automatically with integers unless I do the above steps? C’mon!! But, for serious, when I upgraded from 2005 to 2008…somehow the script I generated didn’t have the “IDENTITY” specs or something… not sure.  Later, I found that adding the identity via SSMS threw an error saying the table would need to be dropped and recreated.  I also found I couldn’t delete the column (I can remove its primary key designation, but it complained if I tried to delete it because “it is in use by other objects”).  So, the fall back is to drop and recreate the table – hence the magical dance outlined above.

Arg.

06
Dec
09

SqlDbType and varchar(max) or nvarchar(max)

The short answer? Use SqlDbType.VarChar or SqlDbType.NVarChar with a size of -1.

For more info, see http://msdn.microsoft.com/en-us/library/bb399384.aspx.  I just put the short answer for those who want… the short answer.

14
Apr
09

Down & Dirty .NET PayPal Buttons?

CAN IT BE?
Yes, my friends it is possible… I think.  Bottom line is, you can design buttons all day long in PayPal and there are various ways to get them working.  However, since I am far too dense for GhostForms, I found my own little path.

GO GET YOUR BUTTONS
Now, I am not going to drill into PayPal buttons… their website will generate both hosted and non-hosted buttons for you.  I am opting for non-hosted because then I can actually have my website generate the buttons based on info in my website/database. Handy.

I HAVE BUTTONS, NOW WHAT?
Once you have your buttons, you can do many things.  What I wanted was to have CONTROL! (Sound FX: Maniacal laughter), so I wanted to generate the parameters for my buttons. But there are a couple caveats:
1) I didn’t want the guts of my button in the HTML of the web page for people to examine and hack.
2) I didn’t want the guts of my button on a URL for folks to hack! Hacker: “What’s that price? I think I will just set that to price=-50.00 so Ted owes ME! Hahaha!”

This means, my users will click a button, and my server code will generate an HttpRequest that goes to PayPal, passing it a POST so that PayPal can come back with their shopping page. It goes like this: 

SOURCES
http://stackoverflow.com/questions/698029/invoking-a-post-to-an-external-site-with-c-httpwebrequest
http://www.csharp-station.com/HowTo/HttpWebFetch.aspx

CODE:
//This goes wherever you need it… button click, page load, etc.
//You need System.IO, System.NET, System.Text

//make a button (just for this demo – see PayPal documents)
//NOTE: ampersands used to make a psuedo URL…

            StringBuilder ParamsAsString = new StringBuilder();
            string Url = “https://www.paypal.com/cgi-bin/webscr“;
            ParamsAsString.Append(“cmd=_xclick-subscriptions”);
            ParamsAsString.Append(“&business=your@paypal.email“);
            ParamsAsString.Append(“&lc=US”);
            ParamsAsString.Append(“&item_name=productname”);
            ParamsAsString.Append(“&item_number=productid”);
            ParamsAsString.Append(“&no_note=1″);
            ParamsAsString.Append(“&no_shipping=1″);
            ParamsAsString.Append(“&a3=25.95″);
            ParamsAsString.Append(“&currency_code=USD”);
            ParamsAsString.Append(“&src=1″);
            ParamsAsString.Append(“&p3=1″);
            ParamsAsString.Append(“&t3=M”);
            ParamsAsString.Append(“&sra=1″);
            ParamsAsString.Append(“&bn=PP-SubscriptionsBF:btn_subscribeCC_LG.gif:NonHosted”);
//convert params to a byte array
            byte[] paramStream = Encoding.ASCII.GetBytes(ParamsAsString.ToString());

//create a request
            HttpWebRequest ppr = (HttpWebRequest)WebRequest.Create(Url);
            ppr.Method = “POST”;
            ppr.ContentType = “application/x-www-form-urlencoded”;
            ppr.UserAgent = “Mozilla/5.0 (Windows; U; Windows NT 6.0; sv-SE; rv:1.9.1b2) Gecko/20081201 Firefox/3.1b2″;
            ppr.ContentLength = paramStream.Length;

//Get request stream and put our parameters there!
            using (var stream = ppr.GetRequestStream())
            {
                stream.Write(paramStream, 0, paramStream.Length);
            }

//Send this to PayPal and get response
            var response = ppr.GetResponse();
            string result;
            using (var sr = new StreamReader(response.GetResponseStream()))
            {
                result = sr.ReadToEnd();
            }

//Write this to the user’s browser
            Response.RedirectLocation = “https://www.paypal.com/cgi-bin/webscr“;
            Response.Write(result);
//Be Done! Don’t process the rest of the page…
            Response.Flush();
            Response.End();

NOTES:
I set the RedirectLocation because I feel guilty.  Also, the Flush() and End() stop the rest of the page from processing.  Since you just wrote a complete HTML page from PayPal, continuing to write more will just be weird!!

Now, you might also notice is that the URL on the web page is still YOUR url, but all the buttons, content and everything else, having been put together on the remote website’s server (PayPal in this case) are all set up to point to the correct place…so you should be good to go.

Happy Coding!

07
Mar
09

Package Load Failure Microsoft.VisualStudio.Xaml

BACKGROUND
Well, if you are like me, you downloaded the Express version of Visual Studio 2008 because you were too impatient to wait the 3 days for the DVD to arrive in the mail.  Then it arrived and took 10 years to install, and after firing up  Visual Studio 2008, you get a “Package Load Failure” siting Microsoft.VisualStudio.Xaml.  Afterwords, you notice there is no XAML intellisense.  After a brief bout of insanity – running around screaming, chewing on the cat’s leg, sending the children to bed at 3pm – you then calmly search the web.

FIX
I removed the express editions. No Go.

I ran the repairs suggested here. No go (and, my default key in my registry was not blank, so I was just trying it because I could).

I ran the devenv.exe /setup suggested here (found it buried in an article):
“If you are still hitting this, I just found a fix.  Using cmd, navigate to the Visual Studio 2008 (9.0) folder in the Program Files folder.  cd to Common7\IDE.  Type, “devenv.exe /setup”

You can open taskmgr to monitor the devenv process.  When it’s done, just reopen visual studio and xml should start rendering.  At least it worked for me.  Good luck.”

I deleted the toolbar temp files (and I can’t find the link – sorry) which are located in C:\Documents and Settings under your username, then look under Local Settings\Application Data\Microsoft\VisualStudio\9.0 (or something similar) and delete the .tbd files there. No go

What worked for me was to download and re-run Visual Studio 2008 Service Pack 1

Now it could JUST be SP1, or maybe a combination of steps – so I would recommend running the last three.  Of course if this obliterates your system, then I am not liable…all this stuff is DO AT YOUR OWN RISK!

Enjoy the XAML Goodness (or so I hope)!