3Sixty Systems
Back to 3Sixty  |   Products & services   |   News  |   Partners  |   About us  |   Case studies  |   Careers  |   Contact us

Using ELMAH with ASP.NET MVC3

by Neil Milner-Harris 25. April 2011 23:26

I've just being implementing ELMAH in a new ASP.NET MVC3 project, it really is a must have for any ASP.NET project. Turns out that there are a some key changes to the exception handling process in MVC3 that cause a bit of grief for users of ELMAH. Whilst poking around for some information I stumbled across a really great blog series (there's 5 posts in total) about implementing ELMAH in MVC3.

I recommend anyone who is interested in using ELMAH, and there really is no excuse for not being interested, should check out Joel's blog here as it is a great step by step guide and overview.

A Simple Entity Framework 4 Problem

by Neil Milner-Harris 5. April 2011 00:15

Right a simple problem this, with a fairly simple solution, but not entirely intuitive or obvious.

Supposing we have a data context called context which is a model of a SQL database which includes a table called Country.

Now we want to create a dropdown for those countries, based on a visibility flag and ordered alpahbetically, simple right?

var countries = from c in context.Countries
                            where c.Visible
                            orderby c.Name
                            select new SelectListItem
                                {
                                    Value = c.Id.ToString(),
                                    Text = c.Name
                                };

Well no. Doing that will garner you the exception:

LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression

This is because the entire Linq statement must be translated to a store expression to be executed against the underlying data store, ToString() is not a supported method for this. (On a personal note I have no idea why ToString() is not supported against a SQLServer data store, I hope they fix that up in a future release.)

The solution is not particularly elegant but it is incredibly simple.

 var countries = from c in context.Countries
                            where c.Visible
                            orderby c.Name
                            select c;
 
 var countriesList = from c in countries.AsEnumerable<Country>()
                     select new SelectListItem
                          {
                              Value = c.Id.ToString(),
                              Text = c.Name
                          };

In this particular example (which could be contracted a little, it just makes more sense formatted this way) we set up the Linq query to get the data we want in the order we want. Then we use that query in a Linq to objects statement beneath. The important point to note is the use of the AsEnumerable<>() method. This forces the original Linq query to actually execute and return the list of relevant countries. This then frees the second Linq query from the Linq to entities restrictions and turns it into a simple Linq to objects query.

Not particularly elegant, but it is simple and it works.

It is worth noting that any method that causes the first Linq to execute would have sufficed in the second query, AsEnumerable just makes the most sense in this particular example.

 

 

 

I've got a really annoying VS 2010 bug

by Neil Milner-Harris 3. September 2010 21:05

Update: My colleague has found a fix for this issue. Uninstall the WOVSS Default Browser extension and reinstalling it fixes the issue.

I have a VS2010 solution that contains 4 Database Projects. All of a sudden in the middle of the day yesterday, having already done some deployments that morning, VS crashed and restarted when I tried to deploy one of the DB projects.

So I reloaded the solution and tried again and it crashed again. Joy!

Bringing up the properties dialog for the project renders the error message:

 

An error occurred trying to load the project properties window.  Close the window and try again.

Cannot evaluate the item metadata "%(FullPath)". The item metadata "%(FullPath)" cannot be applied to the path "obj\Debug|Any CPU\RenatusDev.dbschema". Illegal characters in path.  C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets

 

So one quick google later reveals the following post:

http://blog.projectsoftware.ro/2009/09/database-projects-error-an-error-occurred-trying
-to-load-the-project-properties-window-close-the-window-and-try-again/

Essentially the solution to this issue is to run:

devenv.exe /setup

devenv.exe /resetuserdata

That actually works and fixes the issue, but then you have to spend ages reinstalling all of your extensions and recreating all of your settings.

Fine as a one off but it has just happened for a second time in two days so now I'm looking for any other solutions that don't involve resetting all the VS user data.

Please comment if you've got one....

Discovered a really cool multiple file uploader

by Neil Milner-Harris 3. September 2010 20:30

Andrea Boschin has put together a really cool SilverLight control that essentially just produces a drop area which files can be dragged onto from windows explorer and will be automatically uploaded to the web server.

Its really simple in terms of the functionality that is presented to the user, but extremely useful. The main event being you can select multiple files at the same time and drag them onto the control instead of having to hit browse, navigate to the file and click open, rinse and repeat.

You can find it here:

http://www.silverlightshow.net/items/Uploading-multiple-files-with-Silverlight-4.0.aspx

Unfortunately there is no hosted demo of the tool but the full source code is available for download and simply needs you to press debug in VS 2010 to get it working.

 

Adding SPLookup fields programmatically

by Neil Milner-Harris 2. September 2010 19:27

To get us going on the new corporate blog, here is a repost of something I wrote a while back concerning the addition of SPLookup fields to SharePoint lists.

Here is the scenario:

I’ve got a WSP that creates some lists and some of those lists have lookup and multi lookup fields in them that reference other lists in the WSP.

So to start with I configured the fields in the list definition xml like the documentation says you should. The result, they appeared on the list but any attempt to access them programmatically failed with a variety of generic sharepoint errors. When you tried to access an item through the standard admin pages for the list the field would appear but would not link to the source list.

OK, great, so I don’t have time to figure out why this doesn’t work properly so I decide I will just include some code in the already existing feature event listener to add the fields on feature activation.

So I start with the Microsoft documented route:

sourceList.Fields.AddLookup("SomeField", targetList.ID, false);

Excellent, the lookups appear and link up correctly, I’m starting to feel much happier about my impending deadline.

Now I just need to deal with the multi value lookups, so I just extend what is already working to use some of the methods that exist on the object I’ve just created, right? Wrong!

sourceList.Fields.AddLookup("SomeField", targetList.ID, false);

SPFieldLookup lookupField = (SPFieldLookup)sourceList.Fields["SomeField"];

lookupField.AllowMultipleValues = true;

lookupField.Update();

That should work, it compiles with no warnings so all good. Except that if you try and call SPFieldLookup.Update() after modifying any of it’s properties you get the infamous “Cannot complete this action” error and none of your changes are saved.

After knocking about with this for a short while trying to identify which specific action was causing the error and whether there was some way I could work around it, I came up with this alternative.

private static void AddLookupField(string fieldName, string sourceField, SPList sourceList, SPList targetList, bool isMulti, bool isRequired) 

        { 

            fieldName = fieldName.Replace("//", "_x002f_"); 

            if (!targetList.Fields.ContainsField(fieldName)) 

            { 

                string fieldType = isMulti ? "LookupMulti" : "Lookup"

                string mult = isMulti ? "TRUE" : "FALSE"

                string required = isRequired ? "TRUE" : "FALSE"

                string xml = "<Field Type=\"" + fieldType + "\" Mult=\"" + mult + "\" Required=\"" + required + "\" ShowField=\"" + sourceField + "\" DisplayName=\""+ fieldName + "\" StaticName=\"" + fieldName + "\" Name=\"" + fieldName + "\" List=\"" + sourceList.ID.ToString() + "\" />"

                targetList.Fields.AddFieldAsXml(xml); 

            } 

        }

 

This function basically takes all the parameters you need to supply to create an SPFieldLookup of just about any type you may need and creates the correct xml fragment for it.

In the words of Ron Burgandy, “60% of the time, it works every time”

Tags:

Month List

Powered by BlogEngine.NET 1.6.0.0