Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Articles
Search: 

Bonnie Berent's .NET Tips
Bonnie DeWitt, November 1, 2005
Great tips for .NET developers
Summary
Great tips for .NET developers
Description

Line Numbers in Error Messages
You may wish to use the Exception.StackTrace property in your Error Messages when you use a try/catch to trap exceptions:
try
{
   // Code that will cause an error:
   DataSet oData = null;
   DataTable dt = oData.Tables[0];
}
catch (Exception ex)
{
   string error = "An error has occurred! Do you wish to continue?" + (char)13 + (char)13;
   error = error + "Error: " + ex.Message + (char)13 + (char)13;
   error = error + "Error method: " + ex.TargetSite  + (char)13 + (char)13;
   error = error + "Stack trace" + ex.StackTrace;
   MessageBox.Show(error);
}
This works just fine for displaying the line number of the offending statement when you're in the development phase. But, if you use the defaults in Visual Studio when building the Release version of your application, the line numbers will not be displayed in the Release version. This is because Debugging information is not usually turned on for the Release configuration by default, as it is for the Debug configuration. To change these defaults, right-click the project, click on "Configuration Properties", make sure the Configuration combo is set to "Release", and under the Build settings enable the "Generate Debugging Information" property. You'll need to deploy the .PDB files as well.

from a solution provided by Craig Mcguff in Message #1039268

Deleting Data Rows
Most people are used to looping through rows in a DataView using either for or foreach loops, starting at 0 until the you've finished iterating through all the rows. This works fine except in the case where you're deleting rows along the way. In this case, you should use a for loop and iterate backwards through the DataView.
for(int i= MyView.Count-1; i >=0; i--)
{
    if (MyView[i]["MyCriteria"].ToString() == "DeleteMe")
    {
        MyView[i].Delete();
    }
    else
    {
      // do some processing here
    }
}
So, why does this cause problems if you don't iterate backwards? Because, once a Row is marked as Deleted, it's gone from the DataView (since, typically, DataViews do not include Deleted rows), and if you were iterating forward through the DataView, you'd end up skipping past the row immediately after the one that was just deleted thereby not processing every row in your view. Deleting backwards gets around this problem.

from a solution provided by Bonnie Berent in Message #1048309 and Message #1048341

Browser Rendering Differences
By default FireFox and Mozilla browsers are considered downlevel browsers so they don't use style sheets, which results in very different looking UIs between the two browsers. There are a number of ways to fix this uing the BrowserCaps section in Machine or web.config.

One way is to just default all rendering to the 4.0 version of the HtmlTextWriter:
<configuration>
   <system.web>
    <browserCaps>
         TagWriter=System.Web.UI.HtmlTextWriter
    </browserCaps>
   </system.web>
</configuration>
You can google for a complete browserCaps section that works with Mozilla style browsers that you can use instead of this generic setting.

from a solution provided by Rick Strahl in Message #1021087

Check Items in a CheckedItemsList
In a CheckedItemList, how do you check all the items?
for(int i=0; i<this.MyCheckedListBox.Items.Count; i++)
   this.MyCheckedListBox.SetItemChecked(i, true);
Set the 2nd parm to false if you want to uncheck them.

from a solution provided by Kevin Goff in Message #1030845

Bonnie DeWitt, Geneva Systems Group
Bonnie is currently one of the principals of Geneva Systems Group. Call her the Senior Software Engineer, or even call her the VP of Engineering. She has no official title at the moment. Bonnie has been writing software in various languages for about 30 years. Bonnie's current focus on C# .NET applications began in early 2002. She has been a Microsoft C# MVP since Oct 2003 and an active member of the .NET online community.
More articles from this author
Bonnie DeWitt, September 1, 2005
Great tips for .NET developers
Bonnie DeWitt, October 1, 2005
Great tips for .NET developers
Bonnie DeWitt, December 1, 2005
Great tips for .NET developers
Bonnie DeWitt, April 1, 2009
Great tips from the .NET developer community compiled by Bonnie Berent.
Bonnie DeWitt, February 1, 2006
Great tips for .NET developers
Bonnie DeWitt, March 1, 2006
Great tips for .NET developers
Bonnie DeWitt, April 1, 2006
Great tips for .NET developers
Bonnie DeWitt, May 1, 2006
Great tips for .NET developers
Bonnie DeWitt, June 1, 2006
Great tips for .NET developers
Bonnie DeWitt, July 1, 2006
Great tips for .NET developers
Bonnie DeWitt, August 1, 2006
Great tips for .NET developers
Bonnie DeWitt, September 1, 2006
Great tips for .NET developers
Bonnie DeWitt, October 1, 2006
Great tips for .NET developers
Bonnie DeWitt, November 1, 2006
Great tips for .NET developers
Bonnie DeWitt, December 1, 2006
Great tips for .NET developers
Bonnie DeWitt, January 1, 2007
Great tips for .NET developers
Bonnie DeWitt, February 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, March 1, 2006
Great tips for .NET developers.
Bonnie DeWitt, April 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, March 1, 2007
Good tips for .NET developers.
Bonnie DeWitt, May 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, June 1, 2007
Great tips and tricks for .NET developers.
Bonnie DeWitt, July 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, August 1, 2007
Great tips for .NET developers
Bonnie DeWitt, September 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, February 1, 2008
Great tips from the Universal Thread .NET community.
Bonnie DeWitt, March 1, 2008
Great tips for .NET developers selected from the community by Bonnie Berent.
Bonnie DeWitt, April 1, 2008
Great tips from the .NET developer community compiled by Bonnie Berent.
Bonnie DeWitt, January 1, 2006
Great tips for .NET developers