Thursday 21 October 2010

C#: Capture exception and all inner exceptions


With complex architectures and many layers in the code throwing exceptions, it can prove difficult to find out exactly what went wrong.

Generally, the GUI may produce an error for example "Payment could not be processed." when making a payment on-line. From this error, we don't know which layer caused this error, but we know that along the way, something occurred.

This code snippet takes an exception and loops through all inner exceptions and produces a string for the whole exception. This can be stored in a logfile and the problem can be easily identified.


/// <summary>
/// Gets a full exception message string from a given exception.
/// This will loop through all inner exceptions.
/// </summary>
/// <param name="ex">Incoming exception</param>
/// <returns>Full exception message</returns>
internal string GetFullExceptionMessage(Exception ex)
{
    string result = string.Empty;
 
    while (ex != null)
    {
    result += ex.ToString() + Environment.NewLine;
    ex = ex.InnerException;
    }
 
    return result;
}

Wednesday 13 October 2010

Downgrading Adobe Flash Player


So I had an issue today where all of my SWF video tutorials did not work correctly after a certain version of the Flash Player. I originally used ViewletCam software to do this back in 2004; and the videos seemed to half at random intervals and then crash.

I needed a way to at least view these files locally, so that I could convert them to something more suitable. I had FLV in mind, and I could use these to load into many free web-embedded flash players.

This is what I did.

1. Uninstall the current version of flash. Adobe provides uninstallers for all versions. Current page to do this
2. test that the uninstall worked. This should either: not load or prompt you to install flash player (do not install here) Link here
3. Obtain an older copy of flash player. Adobe has them all Here or take a visit to OldVersions.com
4. Install the old flash player.
5. Repeat step 2... this should give you your new flash version.

Tuesday 12 October 2010

CSS: Hacks in Google Chrome and Safari only...


Here's a nice snippet to use in CSS when it seems like all other browsers are displaying your content correctly besides browsers based on Apple WebKit (Chrome, Safari etc.)

An example of setting css properties in WebKit browsers
/* This works only in Safari and Google Chrome */
@media screen and (-webkit-min-device-pixel-ratio:0) {
    .classNameHere {
        margin: 0 0 1px 0;
        padding: 0px 3px 0px 3px;
    }
    #exampleBorderStyleHere {
        border:solid 1px #000000;
    }
}

Friday 8 October 2010

ASP.NET/JQUERY - Maintain Scroll Position of DIV (example uses a treeview)


Today I had the task of maintaining the scroll position of an asp:TreeView control. Keeping it short and sweet; here's what I came up with.

I noticed that when rendered in the browser, the TreeView is rendered as a div... and from back in the days of remembering window positions after post backs, I knew this wouldn't be much different.

The solution I was working on used JQuery already and I found it useful to pull out references to server controls using the 'endwith' syntax. You can also get client id's, it doesn't matter to an extent.


Code behind
protected void Page_Load(object sender, EventArgs e)
{
    this.AddEvents();
}
 
private void AddEvents()
{
    ScriptManager.RegisterStartupScript(this, this.GetType(), "Load", "PageLoad();", true);
    this.tvOrganisation.Attributes.Add("onscroll", "saveScrollPosition(this)");
}


In the above code i'm attaching an onscroll event to the Treeview. I'm also adding a form load function in javascript so that I can initialise anything in here.


Page Markup
<script type="text/javascript" src="LOCATION OF JQUERY HERE/jquery-x.x.x.js"> </script>
<script type="text/javascript">
    function saveScrollPosition(div) {
        $("[id$='hdnTVScrollYPos']").val(div.scrollTop);
        $("[id$='hdnTVScrollXPos']").val(div.scrollLeft);
    }
 
    function restoreScrollPosition() {
        $("[id$='tvTREEVIEWNAME']").scrollTop($("[id$='hdnTVScrollYPos']").val());
        $("[id$='tvTREEVIEWNAME']").scrollLeft($("[id$='hdnTVScrollXPos']").val());
    }
 
 
    function PageLoad() {;
        restoreScrollPosition();
    }
</script>
 
 
<asp:HiddenField ID="hdnTVScrollXPos" runat="server" Value="0" />
<asp:HiddenField ID="hdnTVScrollYPos" runat="server" Value="0" />


Here i'm using two hidden fields to hold the values of X and Y. This will maintain throughout postback and uses a cookie-less approach towards maintaining these details. The JavaScript speaks for itself.

Monday 4 October 2010

CSS: Cross Browser Gradients


As a Web Developer, my usual ethic is to immediately create an ingenious architecture and spill out reusable code within the framework I've just created; while my design aspects are crudely left to the side until I need a GUI to make the thing work!
In practise, it doesn't really matter which order these are done, just as long as the requirements are in place and there is a general idea.

However, when i'm at home (without the glorious support of a design team to make my images and write my CSS for me), things do get done, but designing pages with a div layout; bearing cross browser functionality in mind and actually making it look good takes a lot of skill. So here's one for the skillset....


Cross Browser Gradients with CSS
.gradientTitle {
    background-color: #CECEF6; /* Opera and browsers that do not support gradients  */
    background: -webkit-gradient(linear, left top, right bottom, from(#CECEF6), to(#FFFFFF)); /* Webkit browsers: Chrome, Safari etc. */
    background: -moz-linear-gradient(left,  #CECEF6,  #FFFFFF); /* Mozilla Firefox */
    filter: progid:DXImageTransform.Microsoft.gradient(GradientType=1, startColorstr='#CECEF6', endColorstr='#FFFFFF'); /* IE 5.5+ */
    padding: 2px 2px 2px 8px;
    color: #1C1C1C;
    width: 60%;
}



I have set an initial background for browsers that do not support gradients. If a browser is capable of picking up one of these three, the background-color is overridden.


Preview

Here's a Sample Heading