Tuesday, 22 March 2011

Profiling, measuring and analysing your Entity Framework code

I love the Entity Framework. Most recently I've been using the model-first approach which allows me to not think too much about databases at all. And despite a few problems with the Migration SQL produced (or rather sometimes not produced) by the Database Generation Power Pack, and some sceptical colleagues, I'm firmly pro-EF.

I'll happily admit that I've yet to use it for a high-demand or high-traffic application, meaning I haven't had to spend too much time considering performance, but running the Entity Framework Profiler from Hibernating Rhinos has proved to be enlightening, revealing some non-optimal queries in my recent projects and gently advising how I might make them better!

It's a doddle to install and use and I have little doubt that it'll come in very handy for some of my imminent projects; I'll be encouraging the people I work with to invest in a license when my trial has ended.

Thursday, 10 March 2011

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack

I got this error when using Response.Redirect in a Try-Catch. I'd never seen it before, but it is resolved by specifying 'false' after the URL like below. This tells the execution of the current page not to terminate:

Response.Redirect("whatever.aspx",false);

Wednesday, 9 March 2011

Redirect users on login according to membership roles

If you wish to send membership users to different locations according to their role, then intervene at the LoggedIn event on your login page like so:

protected void Login1_LoggedIn(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(Request.QueryString["ReturnUrl"]))
  {
  // User.IsInRole won't work here here
  if (Roles.IsUserInRole(LoginUser.UserName, "SysAdmin"))
   Response.Redirect("~/Admins/Default.aspx");
  else if (Roles.IsUserInRole(LoginUser.UserName, "Owner"))
   Response.Redirect("~/Owners/Default.aspx");
  }
 }

Output XML from aspx page

Response.ContentType = "text/xml; charset=utf-8";

string myXML="" +
"<catalog>"+
   "<book id=\"book1\">"+
      "<author>Pitt, Ed</author>"+
      "<title>Another Programming Book</title>"+
    "</book>"+
  "</catalog>";

Response.Write(myXML);

Monday, 7 March 2011

IE8 bug - image max-width and table cells

In a very recent post I described a way to limit image sizes using max-width and max-height. Unfortunately a problem developed with this in IE8 when the images were being displayed in a table. The images would resize correctly, but the table cell wouldn't.

A work-around for the bug was quickly found...
img.myImage { max-width: 100px; max-height: 100px; }
div.imageWrapper { width: 110px;  }
<div class="imageWrapper">
     <img src="images/blah.jpg" class="myImage" />

  </div>
...thanks to my esteemed front-end colleagues and this post from Jon Jungman.

Microsoft encouraging IE6 demise

Microsoft have launched a website ie6countdown.com to encourage and map the shift away from IE6, the bane of web developers around the world. "We know that web developers are spending too much time supporting Internet Explorer 6. We understand, and we're here to help. Join us in moving Internet Explorer 6 users to a modern browser", they say.

This is great news and not a moment too soon. Having the endorsement of Microsoft behind you when you explain to clients that really nobody should be using IE6 carries great weight.

I hope this campaign is a success, and I expect to be seeing (errr, if I was using IE6) this banner on many big-brand sites very soon:

Thursday, 3 March 2011

Setting a maximum image height or width using max-wdth and max-height

I've been building a simple image library for a content management system and part of it requires the listing of images with thumbnails displayed. The image dimensions are highly variable, and so I needed to some way to display the image at its natural size and aspect ratio, but to limit both dimensions so large images couldn't upset the way the list appears.

I used this style setting at the top of my page, imposing the limits on all <img> elements. Naturally this could be made more specific, but this suited me fine.

img {
max-width: 100px;
max-height: 100px;
/* Resize the image for IE6 */
width: expression(this.width > 100 ? 100: true);
height: expression(this.height > 100 ? 100: true);
}