Thursday, March 17, 2005

Just found this code in an app:


Try
   If Not Ok Then
      Throw New Exception
   End If
   flag = True
Catch ex As Exception
   flag = False
End Try

PLEASE Don't do this!
The variable names have been changed to protect the innocence, but this is the exact code :( This is an extreme example of how exceptions can be misused.

Tuesday, March 08, 2005

I just (re)discovered that the javascript form.submit method does not fire the onSubmit event for a form. I discovered it while trying to add some code to save the position on page at postback. I also discovered that ASP.NET code inserts a __doPostBack method that is used for a variety of reasons. Needed to hijack it so that my code got called.

Wednesday, January 05, 2005

This is a cool user-interface implemenation by Google called Google Suggest. From an article on The Code Project it appears that a Javascript object called XMLHttpRequest is used to make server requests for the data.

Wednesday, November 10, 2004

I found this on statement on the Microsoft site that helps clarify the issue I discovered earlier.
Best Practices for Handling Exceptions: "The stack trace begins at the statement where the exception is thrown and ends at the catch statement that catches the exception. Be aware of this fact when deciding where to place a throw statement. "
I was trying to track down an issue I was having with Encompass and was trying to find it based on the stack trace I had. The stack trace was very 'shallow', all I could tell was where the last error was 'rethrown' in a Catch statement.
I discovered that I was using exceptions incorrectly :(

Take for instance this code snippet:


Private Sub Button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
Try
testthrow()
Catch ex As Exception
Dim msg As String
Dim innr As Exception
msg = ex.Message & vbCrLf & "---" & vbCrLf & ex.StackTrace
innr = ex.InnerException
While Not (innr Is Nothing)
msg &= vbCrLf & innr.Message & vbCrLf & "---" & vbCrLf & innr.StackTrace
innr = innr.InnerException
End While
MsgBox(msg)
End Try
End Sub
Private Sub testthrow()
Try
testthrow2()
Catch ex As Exception
Throw

Finally
Debug.WriteLine("finally")
End Try
End Sub
Private Sub testthrow2()
Throw New Exception("Eeek!")
End Sub

The problem code is highlighted in yellow. When the exception is rethrown, the stack trace from the caller (in this case testthrow) is lost! In fact, the stack trace indicates the error occurred in the rethrowing catch block.

The correct way to handle this is to throw a new exception, with the 'caught' expection being set as the inner exception of it.

Catch ex As Exception
Throw New Exception("Better exception", ex)


Tuesday, November 09, 2004

I'm doing a magic show on Saturday and was asked to do balloon animals. I thought "Why not, it can't be that hard". Hmm... I was wrong. It's harder than learning a Zarrow Shuffle.

Friday, May 07, 2004

Have you ever wanted to sum the values in a DataGrid in ASP.NET on the client side as the user enters them?

Here's how --

1) Create a datagrid with a template column that contains a text box in which the user will type the numbers.
2) In the ItemDataBound event for the datagrid, FindControl the text box, and append its ClientID to a module level variable (comma separated)
3) In the prerender event, generate script to sum the controls and generate a client side array of items that are to be summed.

Snippets

In the ItemDataBound event
Select Case e.Item.ItemType
Case ListItemType.AlternatingItem, ListItemType.Item
Dim ctl As Control
ctl = e.Item.FindControl("txtNumber")
If Not IsNothing(ctl) Then
If _NumberTextBoxes <> "" Then
_NumberTextBoxes&= ", "
End If
_NumberTextBoxes &= "'" & ctl.ClientID & "'"
CType(ctl, TextBox).Attributes.Add("onblur", "UpdateTotals();")
End If
End Select

in the Page.PreRender Event
Dim scr As New System.Text.StringBuilder

scr.Append("<script>")
scr.Append("function UpdateTotals()" & vbCrLf)
scr.Append("{" & vbCrLf)
scr.Append(" var tot;" & vbCrLf)
scr.Append(" if (parseFloat(" & DHTMLElement(txtAmountRequested) & ".value).toString() != 'NaN')" & vbCrLf)
scr.Append(" tot = parseFloat(" & DHTMLElement(txtAmountRequested) & ".value);" & vbCrLf)
scr.Append(" else" & vbCrLf)
scr.Append(" tot = 0;" & vbCrLf)
scr.Append(vbCrLf)
scr.Append(" for (var i=0; i<TextBoxArray.length; i++)" & vbCrLf)
scr.Append(" {" & vbCrLf)
scr.Append(" if (parseFloat(document.getElementById(TextBoxArray[i]).value).toString() != 'NaN')" & vbCrLf)
scr.Append(" tot = tot + parseFloat(document.getElementById(TextBoxArray[i]).value);" & vbCrLf)
scr.Append(" }" & vbCrLf)
scr.Append(vbCrLf)
scr.Append(" " & DHTMLElement(lblTotal) & ".innerHTML = tot;" & vbCrLf)
scr.Append("}" & vbCrLf)
scr.Append("</script>" & vbCrLf)

If Not Me.Page.IsClientScriptBlockRegistered("webRequestScript") Then
Me.Page.RegisterArrayDeclaration("TextBoxArray", _NumberTextBoxes)

Me.Page.RegisterClientScriptBlock("webRequestScript", scr.ToString)

Dim scr2 As New System.Text.StringBuilder
scr2.Append("<script>" & vbCrLf)
scr2.Append("UpdateTotals();" & vbCrLf)
scr2.Append("</script>" & vbCrLf)

Me.Page.RegisterStartupScript("webRequestStart", scr2.ToString)
End If

Thursday, May 06, 2004

Found out an interesting fact on Request.Redirect. It actually throws an error! This is a problem if you have some code like this:

try
... do some processing ...

Request.Redirect("another page.aspx")

catch ex as exception
lblError.text = ex.Message
end try


In this code I don't want to redirect if there is an error in the processing before the Redirect.

To fix, code as follows:
try
... do some processing ...

try
Request.Redirect("another page.aspx")
Catch tae As System.Threading.ThreadAbortException
' Ignore this error that is returned by Redirect
end try

catch ex as exception
lblError.text = ex.Message
end try