Thursday, November 20, 2008

Error handling with Global.asax and Update Panel

This morning while working with the Global.asax to handle my application level errors, I faced a very strange problem.

Despite of having a Error Handling routine in my Global.asax file, I was unable to trap an exception from the application and instead it was just throwing up a Message Box with the exception message.

So all my routine of logging the error and recording it into the DB was going without being executed.

After a fair bit of research, the UPDATEPANEL came out to be the culprit.

By its very design, UpdatePanels are designed not to bubble up the exception by default and thus it just pops up a Message Box and thus the Error handling routine in Global.asax was never called.

So to call this and feature a full post-back, we need to handle the AsyncPostBackError event of the Script Manager. Here is the code for that.
1. Add the following attribute to the Script Manager
2. Create the event handler on the code behind
protected void scriptMan_AsyncPostBackError(object
sender, AsyncPostBackErrorEventArgs e)
MethodInfo preserveStackTrace =
typeof(Exception).GetMethod("InternalPreserveStackTrace", BindingFlags.Instance BindingFlags.NonPublic);
preserveStackTrace.Invoke(e.Exception, null);
throw e.Exception;

This would bubble up the event and Global.asax would be able to catch the exception thrown and here we could have our all cleanup routine for error trapping and recording.

-- Ashutosh

Wednesday, November 19, 2008

The row value(s) updated or deleted either do not make the row unique or they alter multiple rows.

This is not a common error to see in the context of today's world, but it is really annoying when it does come up.

This morning while working on a SQL Table, I got this error to come up when I inserted a new row to a table. It did not complain to me when I did an insert on that table, but when I tried to update a few values in that newly added row, it threw up this error.

After a further look down into the issue, I noticed that the table did not had a Primary Key and therefore it allowed me to enter the duplicate records.

Now using SQL Server Management Studio, when I tried to update that row, this error showed up.

Now the worst part, it would not even let you delete that row from the table, since it gets confused which row to delete as there are two rows with exactly same values in the DB.

So below are the two methods to get rid of such an issue.

1. Use "SET ROWCOUNT = 1" when deleting the row.
When you do a
DELETE FROM Table1 where Name = 'ABCD'

it will not work since there are two rows with the value of Name = 'ABCD'
So by simple doing
DELETE FROM Table1 where Name = 'ABCD'

it works as it now looks for the first row that matches the criteria and deletes that.

2. Adding an Identity Column
Another way of tackling this problem is adding a new temporary Identity column that would serve as Primary key and we could safely delete the row based on that PK.

This will give seperate identity values to both the rows and thus one of them could be deleted by using this column in the where clause.