views:

95

answers:

3

I have an ASP.NET site (VB.NET) that I'm trying to clean up. When it was originally created it was written with no error handling, and I'm trying to add it in to improve the User Experience.

Try
    If Not String.IsNullOrEmpty(strMfgName) And Not String.IsNullOrEmpty(strSortType) Then
     If Integer.TryParse(Request.QueryString("CategoryID"), i) And String.IsNullOrEmpty(Request.QueryString("CategoryID")) 
      MyDataGrid.DataSource = ProductCategoryDB.GetMfgItems(strMfgName, strSortType, i)
     Else
      MyDataGrid.DataSource = ProductCategoryDB.GetMfgItems(strMfgName, strSortType)
     End If
     MyDataGrid.DataBind()

     If CType(MyDataGrid.DataSource, DataSet).Tables("Data").Rows.Count > 0 Then
      lblCatName.Text = CType(MyDataGrid.DataSource, DataSet).Tables("Data").Rows(0).Item("mfgName")
     End If

     If MyDataGrid.Items.Count < 2 Then
      cboSortTypes.Visible = False
      table_search.Visible = False
     End If
     If MyDataGrid.PageCount < 2 Then
      MyDataGrid.PagerStyle.Visible = False
     End If
    Else
     lblCatName.Text &= "<br /><span style=""fontf-size: 12px;"">There are no items for this manufacturer</span>"
     MyDataGrid.Visible = False
     table_search.Visible = False
    End If
Catch
    lblCatName.Text &= "<br /><span style=""font-size: 12px;"">There are no items for this manufacturer</span>"
    MyDataGrid.Visible = False
    table_search.Visible = False
End Try

Now, this is trying to avoid generating a 500 error by catching exceptions. There can be three items on the query string, but only two matter here. In my test environment and in Visual Studio when I run this site, it doesn't matter if that item is on the query string. In production, it does matter. If that third item isn't present (SubCategoryID) on the query string, then the "There are no items for this manufacturer" displays instead of the data from the database.

In the two different environments I am seeing two different code execution paths, despite the same URLs and the same code base.

The site is running on Server 2003 with IIS 6.

Thoughts?

EDIT: In response to the answer below, I doubt it's a connection error (though I see what you're getting to), as when I add the SubCategoryID to the query string, the site works correctly (displaying data from the database).

Also, if please let me know if you have any suggestions for how to test this scenario, without deploying the code back to production (it's been rolled back).

+3  A: 

I think you should try to print out the exception details in your catch block to see what the problem is. It could anything for example a connection error to your database.

Jakob Christensen
A: 

The error could be anything, and you should definitely consider printing this out or logging it somewhere, rather than making the assumption that there's no data. You're also outputting the same error message to the UI for two different code paths, which makes things harder to debug, especially without knowing if an exception occurred, and if so, what it was.

Generally, it's also better not to have a catch for all exceptions in cases like this, especially without logging the error. Instead, you should catch specific exceptions and handle these appropriately, and any real exceptions can get passed up the stack, ideally to a global error handler which can log it and/or send out some kind of error notification.

Mun
A: 

I discovered the reason yesterday. In short it was because when I copied my files from my computer into my dev-test environment, I missed a file, which ironically caused it to work, rather than not. So in the end it would have functioned the same in both environments.

MasterMax1313