May 29, 2012
$99 TouchPad running CyanogenMod 9 Nightly. I liked WebOS, but the browsing was buggy. I like Android ICS much better.

$99 TouchPad running CyanogenMod 9 Nightly. I liked WebOS, but the browsing was buggy. I like Android ICS much better.

May 29, 2012
Generating an In Memory Word Document using OpenXml and streaming w/ ASP.NET

There are a lot of links online on how to create word documents on the fly using the OpenXML SDK but they often either target opening an existing document or saving a document on the file system, and furthermore, actually streaming the document down to the user becomes a series of experiments with Content Types, Headers, etc.

Here is some code that is tested and works - I had to write some code recently to stream a Word Document on the fly, and hopefully this post will save someone some time. I actually had to rely on StackOverflow to discover the WriteTo vs CopyTo bug

I learned that Stream.WriteTo is buggy, and not saving the in memory word document before streaming it down causes problems. Additionally if you don’t flush and close your response, Word will likely complain about a corrupt document.

Here is the complete code

 
// Create Memory Stream
using (MemoryStream mem = new MemoryStream())
{
// Create Document
using (WordprocessingDocument wordDocument = 
WordprocessingDocument.Create(mem, WordprocessingDocumentType.Document, true)) { // Add a main document part. MainDocumentPart mainPart = wordDocument.AddMainDocumentPart(); // Add a Body new Document(new Body()).Save(mainPart); Body body = mainPart.Document.Body; // Add some text body.Append(new Paragraph( new Run( new Text("Oh hi.")))); // Save and Close the document - if you don't close, you will have issues : / mainPart.Document.Save(); wordDocument.Close(); } Response.ContentType =
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"; Response.AppendHeader
("Content-Disposition", "attachment;filename=HelloWorld.docx"); mem.Position = 0; mem.CopyTo(Response.OutputStream); // WriteTo is bugged, you have to use CopyTo Response.Flush(); // And you have to Flush and End the response Response.End(); }

September 21, 2011
And there was much rejoicing…

SQL Server (Denali) finally gets an easy way to format dates instead of the magic number game we’ve been playing for years.

September 19, 2011
And it begins

I remember in the early 2000s when the .NET wallet was something Microsoft dreamed of, 11 years later, its a reality. This has big implications.

Link: Google Wallet opens for business, Visa gets on board

8:24pm  |   URL: http://tmblr.co/Z7ZP5y9jG_Cx
(View comments  
Filed under: banking mobile 
September 18, 2011
A win is a win. To Navy: I salute you! To Dr. Hyman: Please don’t schedule a triple option team ever again.

A win is a win. To Navy: I salute you! To Dr. Hyman: Please don’t schedule a triple option team ever again.

8:53am  |   URL: http://tmblr.co/Z7ZP5y9fHVar
(View comments  
Filed under: gamecocks 
September 8, 2011
Razor, Partial Views and supposedly a cache issue

While playing around with Razor I ran into a weird issue that convinced me I had something funky going on with caching and IIS 7.5 and/or jquery itself. 

Long story short, I wanted to check out the new Razor View Engine so I worked on an application that would show random quotations spoken by developers at some point in time.

The basic premise was this:

When the application comes up it bring up a random quote from the database. After a short interval, it would refresh the quote via an ajax call. There are various degrees of trickery you can do to get a random record from the database, but I didn’t want to use the NEWID() trick where you select your data and select and additional NEWID() column and sort by it. I ended up doing something a little different, although some may consider it a little kludgy nonetheless. I used a generic list shuffler, courtesy of StackOverlfow, which avoids using the Random type. If you’re curious here is the implementation:

public static void Shuffle<T>(this IList<T> list)
{
    RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider();
    int n = list.Count;
    while (n > 1)
    {
        byte[] box = new byte[1];
        do provider.GetBytes(box);
        while (!(box[0] < n * (Byte.MaxValue / n)));
        int k = (box[0] % n);
        n--;
        T value = list[k];
        list[k] = list[n];
        list[n] = value;
    }
}


My model was previously defined in the earlier MVC 2 iteration of this demo project so I decided to use a variation of EF Code-First where I wanted to associate this model with an existing database. Anyway, I wired up everything correctly and things were smooth locally on the ASP.NET development server.

My random list shuffler appeared to work. (Note: this is not efficient, I could have actually just got a random quote directly from the DB, but I digress…). I’d was getting all the quotes from the database into a list, and then shuffling them and picking the first one. I isolated the display of the quote itself into a partial view and added the following code for a refresh:

setInterval(function () {
            $('#currentQuote').slideUp('fast', function () { });
            $.getJSON('Quote/GetNew', function (data) {
                $('#currentQuoteText').html(data.Text);
                $('#currentQuoteSource').html('- ' + data.Source);
            })
            $('#currentQuote').slideDown('slow', function () { });
            ;
        }, 7000);


This basically does this, it slides up the container that has the current quote, calls the GetNew action on the Quote controller, and replaces the html elements with the new quote to be displayed.

The partial view was pretty trivial as well:

@model Quoteboard.Domain.Entities.Quote
<div id="currentQuote">
    <h4 id="currentQuoteText">@Model.Text</h4>
    <blockquote id="currentQuoteSource">
        - @Model.Source</blockquote>
</div>


Once I dropped it on IIS, where the server name was actually showing up instead of localhost:port/ as the url for the application, the first quote would load repeatedly. This was insanely annoying because it would work locally. This triggered the red herring of a cache issue.

I did all kinds of silly things like decorating my action with this attribute

[System.Web.Mvc.OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]


I even went into IIS and prevented caching on the application. Yet it would not work, the same quote was being loaded over and over again. Curiously, there were no real error’s I could think of.

Finally, my mind went back to the getJson call. Was it failing? Thankfully, Chrome provides the Javascript console which revealed the error.

Turns out, it wasn’t caching at all, the code was failing, because Quote/GetNew was 404ing when deployed to IIS, because it was looking for the RELATIVE path! 

The correction that needed to be made was changing the $getJson line to:

 $.getJSON('@Url.Action("GetNew","/Quote")',


so the key was the url which invoked the action locally didn’t work correctly when deployed to a server, since Url.Action -> Generates a fully qualified URL to an action method by using the specified action name. Doh!

Oh well. I guess if someone runs into this issue, hopefully they wont suffer my brief folly. 

9:46am  |   URL: http://tmblr.co/Z7ZP5y9HS7fU
(View comments  
Filed under: code CSharp MVC Razor 
September 3, 2011
Kaden and Melanie before the first game of 2011 (Taken with picplz.)

Kaden and Melanie before the first game of 2011 (Taken with picplz.)

August 31, 2011
Working with your new TouchPad

So, with the help of the slickdeals.net HP TouchPad firesale of August 2011 guidance, I got my hands on a HP TouchPad. 

I watched a lot of YouTube videos trying to get a feel of what I was getting. We have an iPad2 at home and I have a Sprint EVO 4G phone, so I’ve pretty much used iOS, Android with HTC Sense and now webOS.

First impressions:

  • The device is a lot heavier than I expected.
  • The complaints about the fingerprints all over are true. Get a case.
  • webOS is nice. I like the card concept and organization of what you are working on into logical “stacks”
  • The browser leaves a lot to desire.
  • The apps are minimal and most don’t work very well. The Facebook Tablet app is painfully slow.
  • There was one halfway decent twitter client, but its pretty basic as well.
  • Battery life is great
  • I understand why they discontinued the device, at the same price point as an iPad they literally had no chance with the masses.

Recommendations when you get your new TouchPad:

Install Preware. It will make your life much easier, and you’ll be able to tweak the device to make it zippy fast.

Here is a good video as well

Install the following tweaks (take from this post on precentral), the speed difference is very noticeable

* EOM Overlord Monitoring

* Muffle System Logging

* Remove Dropped Packet Logging

* Unset CFQ IO Scheduler

* Unthrottle Download Manager 

* Quiet powerd Messages

* Faster Card Animations HYPER Version

* Increase Touch Sensitivity And Smoothness 10 

I did install the “Make It So” patch because its much cooler than “Just type”

All in all, I’m happy with the purchase, at $99.00 it was definitely a steal. I will mainly use it as a browser/eBook reader and for when I need to view a site that requires flash.

One more thing I really like is the top right hand side has a drop down where you can adjust brightness and lock screen rotation. Its very convenient. iOS and Android make it a little more cumbersome. 

7:50pm  |   URL: http://tmblr.co/Z7ZP5y8_w3G5
(View comments  
Filed under: hptouchpad 
August 31, 2011
I got a HP Touchpad during the great firesale of August 2011. Here is a screenshot of the main screen. I&#8217;ll post my thoughts and a couple of things I did to it soon.

I got a HP Touchpad during the great firesale of August 2011. Here is a screenshot of the main screen. I’ll post my thoughts and a couple of things I did to it soon.

7:24pm  |   URL: http://tmblr.co/Z7ZP5y8_rrCW
(View comments  
Filed under: hptouchpad 
August 31, 2011
Minifying your .js and .css files with Powershell and ajaxmin

I looked into the AjaxMin project on CodePlex recently after a tweet from @shanselman. In a nutshell, it’s a minifier for certain files that can help with web application performance.

Anywhoo, here is a little powershell script I wrote todayto recursively search a starting directory and minify all .js and .css files that it finds.

Warning: this uses the clobber option, so your files will be overwritten. Also, the path to the AjaxMin will differ based on your installation. 

Clear-Host
$Directory = “C:\Path\To\YourRootFolder”
$Minifier = “C:\Program Files (x86)\Microsoft\Microsoft Ajax Minifier\AjaxMin.exe”

get-childitem -recurse -force -include *.js, *.css | foreach-object {&$Minifier $_.FullName -out $_.FullName -clobber}

If you run it without the clobber option it will just show you the output of what your compressed file will look like when running on command line.

Its pretty neat. Hopefully someone will find this useful at some point.