Thursday, March 15, 2007

Most SQL Server runtime errors DO NOT rollback transaction. Solution.

Many developers mistakenly believe that any error inside a transaction will make the transaction fail and automatically rollback. However, this is true only for fatal errors, such as most errors with severity equal to 16 or higher. The commonest runtime error - for example duplicate primary keys, violation of unique constraints and foreign key relationship - aren't fatal and therefore won't rollback the transaction.


To rollback a transaction when an error occurs you must manually check each statement for runtime error using the @@ERROR global variable, as in the following code snippet:


BEGIN TRANSACTION
INSERT authors VALUES (…)
IF (@@ERROR <> 0) GOTO on_error
INSERT publishers VALUES (…)
IF (@@ERROR <> 0) GOTO on_error
COMMIT TRANSACTION
GOTO continue

on_error:
ROLLBACK TRANSACTION

continue:
.....


If the transaction is inside a stored procedure, you might use the procedure's return value to signal success or failure to the caller:

CREATE PROC RunThis AS

BEGIN TRANSACTION

INSERT authors VALUES (...)
IF (@@ERROR <> 0) GOTO on_error
INSERT publishers VALUES (...)
IF (@@ERROR <> 0) GOTO on_error

COMMIT TRANSACTION
-- return 0 to signal success
RETURN (0)

on_error:
ROLLBACK TRANSACTION
-- return 1 to signal failure
RETURN (1)


NOTE: As an alternative, you can use the SET XACT_ABORT ON command, which causes transactions to fail when any runtime error occurs, not just fatal ones. In this case you don't need to test each statement using @ERROR.


Gaia AJAX Widgets, The Alternative To ASP.NET AJAX


Gaia Ajax Widgets is an Ajax library which is far easier to use, way easier to extend and consumes A LOT less bandwidth than most other Ajax libraries including ASP.NET Ajax. It comes with a free (as in free beer) version and a free (as in freedom, Source Code distro) version though is still a commercial library living of license earnings. The company creating Gaia is called Frost Innovation and is a VERY small company (3 employees) and is located in Norway.

It is the first Ajax library for ASP.NET that doesn't send HTML over its Ajax Request but have created Client-Side implementation of the WebControl class hierarchy in JavaScript and can therefore update specific properties of widget "objects" on the client instead of sending HTML to UpdateAble Controls or "UpdatePanels".

Gaia Ajax ASP.NET Widgets

AutoCompleter
Button
CheckBox
DateTimePicker
DropDownList
DynamicImage
HiddenField
HyperLink
Image
ImageButton
InPlaceEdit
Label
LinkButton
ListBox
Menu
MultiView
Panel
PlaceHolder
RadioButtonList
RequiredFieldValidator
TextBox
Timer
Window
View our samples

You can see online a very complete list Gaia controls in action, more information and FREE download at ajaxwidgets.com.


Wednesday, March 14, 2007

ASP.Net Productivity Secrets

There are times that you have to think outside the box to get your designs to work. I've been working on a set of server controls that handle view/add/edit capabilities on a n-tiered vertical table set. You feed the primary control a Category, point it at DataSets containing the schema and the values, and tell it what natural key to use to select the values to be displayed. As you get deeper into the recursion of the data the sub-controls still need to know what the root control is.

The tree built by the data is displayed by recursively adding new instances of the root control to represent a branch in the tree. Since ASP.Net only knows of about the control that are defined in the ASPX page, it becomes necessary for child controls spawned by the tree need to register their events through the root control.

Even though there are better ways to accomplish this, I decided to add a RootControl parameter which is given a reference to the control defined in the ASPX whenever the tree branches. This works fine when you can place code in the Code-Behind to set this property, but what if you are adding a new root control dynamically, such as in a repeater?

Uh, Oh!


<itemtemplate>
<cc1:control runat="server" rootcontrol="???" />
</itemtemplate>


Secret 1, the target object

ASP.Net takes ASPX code, which is a loose form of XML, and generates C# (or VB.Net, or some other language) from the tags defind as above. Inside this code, ASP.Net creates a new method to render each control, and inside this method the control is instantiated with the name target.

The next part of the secret is that the DataBinding context actually inserts code as an expression directly into the code of this new method. This is the little bit we need to create a self-referencing ASPX tag.

Oooh, that's cool!

<itemtemplate>
<cc1:control runat="server" rootcontrol="" />
</itemtemplate>

ASP.Net can now create the self reference that the control structure requires to render the tree!


Secret 2, ASPX as C#

There's a lot of really good info that can be discerned about how ASP.Net works when you look at the source code created when your ASPX page is parsed. You can see this code by opening the Windows Explorer and navigating to "X:\Windows\Microsoft.Net\Framework\v1.0.3075\Temporary ASP.Net Files". You may find this directory in a slightly different location depending on the version of Windows and version of the .Net Framework installed.

Once you're there, you'll find a directory for each ASP.Net application on your webserver. Just start browsing through this directory structure and you'll run across a ton of C# (or VB.Net or J#) source code files with 8-character gibberish for filenames. These are the source files created when ASP.Net converts a page from ASPX to source code, and these files will give you tons of insight into what's going on under the hood with ASP.Net.


Secret 3, ASP.Net and the GAC

Here's another little dilemna you can find yourself in; occasionally when doing development of shared assemblies you run across a situation where you replace the shared assembly in the GAC, but an ASPX page uses a object that is drastically changed in the shared assembly. ASP.Net only recompiles the ASPX page when it changes, which leaves you with a broken reference to the old shared assembly. You can easily recompile your code-behind to use the new version of the shared assembly, but short of touching each ASPX page and resaving them, ASP.Net seems to resist all attempts to bind to the new shared assembly.

The fix is actually very easy. Simply locate the ASP.Net application's directory in the Temporary ASP.Net Files directory, delete it, then revisit the page on the website. Oila! ASP.Net will recompile all your ASPX pages, and they'll have correct references to the new shared assembly!

AJAX Activity Indicators

indicator.gif indicator_arrows.gif indicator_arrows_black.gif indicator_arrows_circle.gif indicator_bar.gif indicator_bar_small.gif indicator_black.gif indicator_bouncing_ball.gif indicator_circle.gif indicator_circle_ball.gif indicator_circle_ball2.gif indicator_circling_ball.gif indicator_flower.gif indicator_green.gif indicator_hypnotize.gif indicator_kit.gif indicator_medium.gif indicator_mozilla_blu.gif

indicator_mozilla_yellow.gif indicator_progress_circles.gif indicator_radar.gif indicator_refresh.gif indicator_remembermilk_orange.gif indicator_snake.gif indicator_square.gif indicator_tiny_red.gif indicator_waitanim.gif indicator_wheel1.gif indicator_wheel2.gif indicator_wheel3.gif indicator_wheel4.gif progressbar_green.gif progressbar_long.gif

progressbar_short.gif indicator_technorati.gif progressbar_microsoft.gif progerssbar_editnplace.gif progressbar_long_green.gif

indicator_web20_working.gif indicator_smallwaitanim.gif indicator_3drotation.gif indicator_little01.gif indicator_little02.gif indicator_circle_thickbox.gif indicator_verybig.gif indicator_big.gif spinning_wheel_throbber.gif loading_animation.gif live_com_loading.gif loading_animation_liferay.gif ms_progcircle.gif desmm_load_w.gif

desmm_load_b.gif aero_dark.gif aero_light.gif





Google