Archive for the ‘microsoft’ Category

Upload to ASP.NET from HTML, Flash, or Flex clients

File uploading has been a hot topic during my time as an internet programmer.  In the classic ASP days this was a bit of a task to build and get correct.  Nowadays both Adobe’s Coldfusion and Microsoft’s ASP.NET both have built in file uploader tags ( server controls ) that handle this with ease.

This is great, but what happens when you have a mixed bag of clients that all need to upload to the same location?  Sometimes I work with completely ASP.NET or CF web apps, but more often than not I’m dealing with Flash clients as well as HTML clients.

Recently I ran into this upload scenario and built this simple ASP.NET uploader script.  This feels a bit old school since it uses .NET’s built in Request.Files collection, instead of a fancy new ‘all in one’ server control, but I actually prefer this method.

Here’s all you need :

    // Check for posted files
    for (int xx = 0; xx < Request.Files.Count; xx++)
    {
        // UPLOAD FILE
        HttpPostedFile _file = Request.Files[xx];

        // create full save path for uploaded file
        var full_file_path = Server.MapPath( UP_FOLDER ) + "\\" + _file.FileName;

        try
        {
            // save file to server
            _file.SaveAs(full_file_path);
        }
        catch (Exception exc)
        {
            var emsg = "Unable to upload file : " + exc.Message;

            Response.Write( emsg );
            Response.Flush();
            Response.End();
        }

        // show result
        Response.Write( _file.FileName + " uploaded! <br>" );
    }

That’s all there is to it codewise. Before using this code you will need to give the NETWORK SERVICES user write permissions to your upload folder. Other than that, that’s all she wrote!

Here is a zip of all the code for you to download.

Inside this zip you will find :

  • flashclient.fla – Flash upload client ( *be sure to update the upload path before building )
  • flexclient.mxml – Flex upload client ( *also update upload path before building )
  • uploader.aspx - ASP.NET file upload handler
  • uploadform.html - sample HTML upload form ( again, update path )

Hope somebody finds this useful.

How to show line numbers in Visual Studio 2010

I’ve been using Visual Studio since forever, yet it always takes me a while to remember how to show line numbers.  It’s especially hard to remember after a fresh install of Visual Studio.  Assuming you have it installed and open, here’s how to display line numbers in your code.

  1. Click Tools in the menu bar
  2. Options
  3. Expand Text Editor ( in the popup window )
  4. Click ‘All Languages’
  5. Check the ‘Line numbers’ box under the Display heading ( on the right )
  6. Click OK
  7. Happy Happy Joy Joy!

How to Display Line numbers in Visual Studio 2010

Add Eclipse’s Open Resource to Visual Studio 2010

One of my favorite features of the Eclipse IDE is ‘Open Resource’ ( Ctrl + Shift + R  ).

Ctrl + Shift + R > opens this sweet timesaver

Don't point and click to your files, just type their name

If you’re unfamiliar with this, it’s a File Open dialog that let’s you type the name of the file you’re looking for, instead of requiring you to point and click your way to the file. This is one of the few features I still can’t believe Visual Studio doesn’t have built in. Now I’ve had other MS experts show me similar “quick find” features of Visual Studio, but it’s still not as easy as Ctrl+Shift+R > type the filename.

When I was using Visual Studio 2008 I came across the Sonic File Finder plugin and I was hooked.  Then I upgraded to Visual Studio 2010 and my plugin went away.  Today I solved my quick open plugin issue by browsing the Visual Studio Gallery and installing Quick Open File.  This quick open plugin does exactly what Eclipse’ Open Resource does, and it’s a good bit simpler than Sonic File Finder.  Now that I’ve got the plugin installed, the next step is to configure Visual Studio to open this plugin when I hit the Ctrl + Shirt + R keyboard combination.

Add Ctrl+Shift+R to Visual Studio

  1. Fire up Visual Studio
  2. Click Tools > Options > Environment > Keyboard
  3. You should now be at the window for assigning keyboard shortcuts

    This is where you edit keyboard shortcuts in Visual Studio

  4. Type “Quick” into the Show commands containing box
  5. Click inside the “Press shortcut keys” box, and then press Ctrl + Shift + R on your keyboard
  6. Assuming you’ve set this to Global, you are now good to go.

**NOTE : When assigning a keyboard shortcut in Visual Studio, you want to make sure your new shortcut isn’t already assigned to a different command.  If this is the case, you should remove your shortcut assignment from the other command before assigning to your new command.  This dialog will show you what is already assigned to a keyboard combination like so.

Make sure your new shortcut isn't already assigned

Assuming you made it this far, pressing Ctrl + Shift + R in Visual Studio should now show you this Quick File Open dialog.

Visual Studio 2010 plugin, 'Quick Open File' dialog box

There you go, quick open in Eclipse and Visual Studio!

What if you want to PIVOT against a text column?

If you’ve ever worked with or researched SQL Server’s PIVOT function, you probably noticed most of the samples pivot against an id column.  Typically an int column like EmployeeID, or StoreID.  That’s fine and dandy, but what happens when you want to PIVOT against a varchar column?  If you’ve been in this need you know this is a bit of a task.

I had this need on an app recently and built a little dynamic sql action that does just this.  The example below however, uses the the DatabaseLog table in the AdventureWorks sample database to return a count of Events logged for each Schema.  Before jumping into the PIVOT, here’s a simple query that gives you the same information, all Schemas, Events, and Event counts.

SELECT      [Schema], [Event], COUNT( [Event] ) AS 'event_count'
FROM        DatabaseLog
GROUP BY    [Schema], [Event]
ORDER BY    [Schema]

Running this query should give you a long result looking something like this.

Data is there, format isn't nice like PIVOT

While this query returns the same information to you, I don’t like this format as much as using PIVOT.  This query result is long and requires a bit of manipulation to get into a readable format.

Now let’s have a look at retrieving the same information using the PIVOT function.

/*
Example of a dynamic PIVOT against a varchar column from the Adventureworks database

References :
PIVOT & UNPIVOT function

http://msdn.microsoft.com/en-us/library/ms177410.aspx

AdventureWorks sample Databases

http://msdn.microsoft.com/en-us/library/ms124501(v=SQL.100).aspx

AdventreWorks.DatabaseLog

http://msdn.microsoft.com/en-us/library/ms124872.aspx

*/

USE AdventureWorks

-- populate temp Event table
SELECT DISTINCT [Event] as 'Event'
INTO	#events
FROM	DatabaseLog

-- this var will hold a comma delimited list of [Event]
DECLARE	@eventList nvarchar(max)

-- create a flattened [Event], list for the PIVOT statement
SELECT	@eventList = COALESCE( @eventList + ', ', '') + CAST( QUOTENAME( [Event] ) AS VARCHAR(1000) )
FROM	#events
ORDER BY [Event]

-- drop table var since our data now lives in @eventList
DROP TABLE #events

-- this var will hold the dynamic PIVOT sql
DECLARE @pvt_sql nvarchar(max)

-- NOTE : we're using dynamic sql here because PIVOT
-- does not support sub SELECT in the 'FOR Event IN ( )'
-- part of the query.
-- If we don't use dynamic SQL here, the PIVOT function
-- requires you to hard code each 'Event'
-- Using SELECT * here so the [Event] columns are auto included
SET @pvt_sql = 'SELECT	*
                FROM
                (
                    SELECT	[Event], [Schema]
                    FROM	DatabaseLog
                ) AS data
                PIVOT
                (
                    COUNT( Event )
                    FOR Event IN
                    ( ' + @eventList + ' )
                ) AS pvt'

-- run the query
EXEC sp_executesql @pvt_sql

Assuming you have the AdventureWorks database installed on your server, running this sql should give you a result looking something like this.

Dynamic PIVOT on text column Event

Show all Schemas and count of each Event type

This query result was truncated to fit in this post, but just know the query above creates a column for every Event in the Databaselog table.

A quick explanation of what’s happening in this sql

  1. First you fill a table variable ( #events ) with all Events from DatabaseLog
  2. Next create a comma delimited list of the Events inside of the table variable
  3. Drop the table variable now that we’ve got our delimited list of Events
  4. Build the PIVOT statement as a string so you can inject the Events list
  5. Fire the dynamic SQL via EXEC

Dynamic SQL is something that comes in handy from time to time, but I do my best to only use it if I absolutely have to.  In this case we’re using it because the PIVOT function does not allow sub SELECT statements.  This is also why we create a specially formatted delimited list of Events prior to building the dynamic sql.

So there you have it, one example of using PIVOT against a varchar column instead of an integer column.  Also, this is a pretty good example of a dynamic PIVOT since it’s pretty simple.  I hope this makes sense, and if you have any suggestions of better techniques, I’d love to hear it.