Monday, February 28, 2011

Using Code Behind For Property Binding in WPF

This is a very common thing in WPF, but after being away from WPF for some time, I often have to look it up. In order to bind to properties in the code behind, you need to set the DataContext of the window to "self" as per example below:

<Window x:Class="MyProject.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">

        <Label Content="{Binding StatusText}"/>
</Window>

Now you can bind to a dependency property like this in your code behind:

public string StatusText
{
    get { return (string)GetValue(StatusTextProperty); }
    set { SetValue(StatusTextProperty, value); }
}

public static readonly DependencyProperty StatusTextProperty =
    DependencyProperty.Register("StatusText", 
typeof(string), typeof(MainWindow), new UIPropertyMetadata(""));

Thursday, February 24, 2011

Returning a Captcha Image as a Result from ASP.NET MVC

Recently I was trying to generate a simple Captcha for ASP.NET MVC. I looked at MVC ReCaptcha, which is a great tool, but I didn't want a confusing ReCaptcha for simple form validation. My goal was to use something very simple to prevent SPAM, but at the same time provide a seamless enough experience to not scare off human users.

Next, I came across this article - Simple CAPTCHA, Create your own in C#. It provides an excellent example on how to generate a simple Captcha. My next task was to convert this to ASP.NET MVC code (I used ASP.NET MVC 3.0). Documentation on this might be a little confusing, but the process ended up extremely simple.

The key that I learned here, is that you need to return FileContentResult. Initially I tried returning FileResult with no success.

    public class CaptchaController : Controller
    {
        public ActionResult GetCaptcha()
        {
             var captcha = GetCaptchaImage();
             var result = new FileContentResult(captcha, "image/png");
             result.FileDownloadName = "captcha.png"; // optional
             return result;
         }
         
         
          private Byte[] GetCaptchaImage()
          {
             // Code to generate your Captcha
             // I used the logic from Raymund Macaalay's blog
             // linked above
             ... 

             var oBytes = oMemoryStream.GetBuffer();
             oMemoryStream.Close();
             oMemoryStream.Dispose();
           
             return oBytes;
         }
}

Once you have this code, you can reference your image from views:

        <img src="/Captcha/GetCaptcha/anything_you_want.png" />

One more thing to note, you can put any file name at the end. This part gets completely ignored by your controller (unless you modify it to take a parameter and do something with it). And that's it!

Tuesday, February 22, 2011

Sitecore 6 and IIS6 form postback causes layout redirect

Recently, wile working on a legacy website build on Sitecore 6.0 and IIS6, I ran into a problem with form posts / post back. On postback, instead of seeing the actual page, I was seeing the layout page being posted to. This threw a 404 error.

After a bunch of research, it turned out that the issue was a missing App_Browsers folder. That folder contains Form.browser file, essential for the postback to work properly.

Thanks to Neil Pullinger's blog entry, I was able to get postbacks working correctly.

Monday, February 21, 2011

Finally - a dark color scheme for VisualStudio2010 that doesn't suck!

I've been using a dark theme for VS2010 for a while now, but especially with new releases of technologies such as ASP.NET MVC and Razor view engine, I started to notice that the scheme becomes unreadable and tough to deal with in certain situations. Finally someone stepped up and found a solution to this problem.

My favorite theme is Rob Conery's take on the Vibrant Ink for TextMate, but because it was released some years ago, it's lacking some of the new feature support. I found an updated version of the theme as well as a gold mine for VisualStudio themes in general - check out Wake Road Ink. For other themes check out Studio Styles.

Tuesday, February 15, 2011

VisualStudio 2008 crashes attaching to process for debugging - Fixed!

A lot of people have written about this HotFix but, I found that it's not easy to find, since the route cause is so obscure. I had a problem when VS2008 would crash when attaching to IIS process w3wp.exe. This would happen somewhat inconsistently, sometimes only once per session. Turns out that this is likely because I have my solution explorer window undocked. If you are like me, and you have multiple monitors or you just undock your solution explorer window to make more space for your code, you are likely experiencing the same problem. This HotFix solved it for me. I hope it helps someone else out as well:

KB960075 - VS Dev Environment crash after undocking windows or changing layouts

Monday, February 14, 2011

Displaying errors in PHP

If you are looking for a way to display errors in PHP on a server where you don't have access to the php.ini file, this is the best way to do it:

error_reporting(E_ALL);
ini_set("display_errors", 1);

I was doing some development on a shared hosting environment, and on error I was seeing blank white screen. The code above allowed me to see what's going on.

Friday, February 4, 2011

Update on T-SQL table from another based on ID

This is a simple SQL script I have to use now and again, but often forget the right syntax for. It uses a join between two tables to update a specific column in one table with the values in the column of the other table.

UPDATE
    table_one
SET
    some_column = t2.some_column
FROM
    table_one t1
INNER JOIN
    table_two t2
ON 
    t1.some_id = t2.some_id