Bad Error Handling by Visual Studio 2010

SNAGHTML40442868

Something bad happened. I’d like to tell you more about it, but only if you can remember to start Visual Studio with an additional command-line parameter, and then recreate the conditions that caused it.

Even a stack trace would have been more useful than the general “Oh, crud” dialog.

Or, just hit the OK button. Smile with tongue out

image

(I’ve noticed this error happening with some regularity now that I’ve been exploring ASP.NET MVC 3 with Razor syntax more. Have you seen this error?)

Translating Controller, Action, and Route Data to a JavaScript Object in ASP.NET MVC 3

To enable a more rich JavaScript/Ajax experience on a web page, I had need of more detailed information regarding the route that resulted in the current View being displayed. I checked around a few sites, and nothing popped out as obviously awesome.

As I was putting the JavaScript in the Layout/Master page, I had few direct assumptions about the location of the View/route that was currently being executed.

Here are the two options I created this evening.

Option one is admittedly more limited, but it serves to demonstrate the basic technique:

<script type="text/javascript">
        
    var _servedFromUrl = "@Url.RouteUrl(this.ViewContext.RouteData.Values)";
    @{ 
        string action = ViewContext.RouteData.Values["action"].ToString();
        string controller = ViewContext.RouteData.Values["controller"].ToString();
    }
    var action = "@action";
    var controller = "@controller";
    
</script>

Note that the code above is contained within a <script> block, as the final destination goal is to make these values available within JavaScript in the browser.

Above, there are actually 2 different options. The first option, results in a string that could look something like this:

/Discussions/Edit/5522

It could be useful to someone. However, it wasn’t useful to me. The next step was to discretely retrieve the action and controller. As you can see, these result in two JavaScript variables, aptly named, action and controller.

var action = "Edit";
var controller = "Discussions";

In this case though, I wanted access to all the values, in a more natural JavaScript format: A JavaScript Object with named (ad-hoc) properties.

<script type="text/javascript">        
    var route = {
        @{                 
            string comma = "";
            foreach (var name in ViewContext.RouteData.Values.Keys)
            { 
                string val = ViewContext.RouteData.Values[name].ToString();
                @: @comma'@name': '@val'
                comma = ",";
            }    
        }                   
        };
</script>

The code above loops through all of the RouteData Keys, and adds each and the corresponding value to a JavaScript object instance named route.

The results:

<script type="text/javascript">
    var route = {             'id': '5522'
                 , 'action': 'Edit'
                 , 'controller': 'Discussion'
    };

</script>

Simple, with discrete values.

WebMatrix and SQL Server Compact 4.0 Table Editing workaround

Occasionally, I encounter the following error when editing a  SQL Server Compact 4.0 database table within WebMatrix.

Alter table only allows columns to be added which can contain null values. The column cannot be added to the table because it does not allow null values.

image

The issue arises when adding a new column to an existing table, and specifying that the column should not allow Nulls (Allow Nulls = False).

The fix isn’t as obvious as I’d like, but it’s simple. In this case, the table does not have any data, which added further confusion. I understand that if I were to try to add a column to a table that had data, and the column was set to require a value, that it doesn’t make much sense (if the column is required, what value would be associated with the column for existing rows?).

However, the table is empty. There is a simple work around however.

  1. Set Allow Nulls to False.
  2. Put in a Default Value, appropriate for the Data Type specified.
  3. Save the table (CTRL+S)
  4. Remove the Default Value.
  5. Save the table (CTRL+S)

(I’m using WebMatrix until VS 2010 includes non-beta support for editing SQL Server Compact 4.0 databases)