Logging Errors & Robust Sample Projects

Get the Help You Need

asp:Q&A

LANGUAGES: VB.NET

ASP.NET VERSIONS: 1.x

 

Logging Errors & Robust Sample Projects

Get the Help You Need

 

By Josef Finsel

 

Welcome to the asp:Q&A column. Before I tackle this month s questions, I want to remind you that this is your column it runs on the questions you ask, so feel free to send them my way so I can help you.

Q: What technique do most folks use for logging errors on a live production server? Does anyone go whole hog and use the Microsoft application block for error logging, or do most people simply use the event log?

 

A: How people log errors depends greatly on why they want to track the errors, but using Microsoft s Exception Management Application Block is a very helpful tool and one that serves multiple purposes. Microsoft s Application Blocks are part of Microsoft s Patterns and Practices (http://msdn.microsoft.com/library/en-us/dnanchor/html/Anch_EntDevAppArchPatPrac.asp). There you can find several application blocks, from data to exception management; be assured this is reusable, debugged code that you can use in your projects (see Figure 1).

 


Figure 1: Get application blocks from MSDN.

 

As the saying goes, Every time you try to reinvent the wheel, you run the risk of inventing something that won t roll. These application blocks are as basic as the wheel things that most programs need. Even better, you aren t simply downloading a tool to use, but rather, you get the source code that you can modify if you need to. As downloaded, the Exception Management Application Block writes the errors to the event log. Accompanying documentation allows you to create custom publishers, which allows you to record errors to a database or send them as an e-mail.

 

Implementing the block is as simple as adding a reference to the block in your code and then implementing it in your Try..Catch statement using:

 

ExceptionManager.Publish(ex)

 

You can download a sample Web page that demonstrates this technique (see end of article for details).

 

Q. I am looking for the most robust SQL Server/ASP.NET sample code project that can be downloaded from the Web. I am looking for something more substantial than the ASP.NET samples. I need to learn this ASP.NET ASAP and most books take a week or two and don t cover anything in detail.

 

A. Again, I would point you toward the Application Blocks with their source code and common programming solutions. I also recommend the Starter Kits found on http://www.asp.net. These kits are full of solutions in VB.NET and C# that range from Time Tracking to a Commerce site. I use several of these kits and find they do a good job teaching the basics if you are willing to look through the code.

 

Q: I have a datagrid with templated columns. I ve used a data adapter and typed dataset, and data bound the items I want to display to my users in the templated columns. My problem is selecting a row and performing some other action. Not an edit or details. I want to capture the values of the row selected, specifically to save the values in a session object. I ve tried a number of methods, all without success. Can you help?

 

A. This is one of those non-intuitive things. If the grid were in a Windows application it would be a simple matter of using the click event. But, because the grid that shows up in a browser is nothing more than a table built using and tags, we need to give the user something to click on; my choice is an asp:ButtonColumn. The button column gives the user something to click on and the programmer something with which to program.

 

For my sample, I m going to create a quick little page that queries the Northwind database and lists several of the columns in the view Alphabetical list of products . I ll display these selected columns in a table using a template. The HTML code for this is simple, as shown in Figure 2. I m displaying the columns using the asp:BoundColumn control and specifying the data column and the header text. The trick to answering this question comes in the form of the first column, the ButtonColumn. This will display a button in each column with the text Select in it. This will automatically call the DataGrid s ItemCommand method, which is where the actual code for storing the data will go.

 

 AutoGenerateColumns="false">

 

 

  CommandName="SaveToSession" Text="Save">

  

  

   HeaderText="ProductID">

  

   HeaderText=" ProductName">

  

   HeaderText=" Quantity/Unit">

  

   HeaderText=" Unit Price">

  

   HeaderText=" Units In Stock">

  

   HeaderText=" Units On Order">

  

   HeaderText=" Reorder Level">

  

   HeaderText=" Discontinued">

  

   HeaderText=" Category Name">

 

 

Figure 2: Displaying data using a DataGrid and templates.

 

The ItemCommand method gets passed two parameters: the source and the DataGridCommandEventArgs (which is the one with which we will be concerned). The two main properties of the DataGridCommandEventArgs that interest us are CommandName (containing the string we defined in the asp:ButtonColumn tag) and Item, which will give us access to the cell data. If you had multiple ButtonColumns you would probably want to structure your code as a Select Case statement. But because we only have one, we ll use a simple If statement and then read through the columns and post them to the session:

 

If e.CommandName = "SaveToSession" Then

 Session.Add("ProductID", e.Item.Cells(1).Text)

 Session.Add("ProductName", e.Item.Cells(2).Text)

 Session.Add("Quantity", e.Item.Cells(3).Text)

 Session.Add("UnitPrice", e.Item.Cells(4).Text)

End If

 

And that s all there is to it. The code that handles all of this is available for download (see end of article for details).

 

That s it for this month. Send your ASP.NET questions to [email protected] so I can help get you the answers you need.

 

The sample code accompanying this article is available for download.

 

Josef Finsel is a software consultant. He has published a number of VB and SQL Server-related articles and is currently working on the syntax for FizzBin.NET, a programming language that works the way programmers have always suspected. He s also author of The Handbook for Reluctant Database Administrators (Apress, 2001).

 

 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish