Current .Net Version: 2.0.50727
Home
Articles
C# & .Net Framework Reference
Practice C# Online
XML Web Services
RSS Feeds
Code Snippets
T-SQL Scripts
Videos
Valuable Links
Contact Us
Mike's C#, VS.NET and SQL Blog




ROR XML Info.
Don't Use BindingManagerBase's Current Property in Expressions
Author: Michael G.     Last Updated:3/28/2005 6:14:01 PM
Category(ies): Windows Forms
Description: Using the BindingManagerBase's Current property in an expression can spell trouble for your Windows Forms applications.
  Add & View Comments About this Article

I noticed the other day that using the BindingManagerBase's Current property in an expression can spell trouble for your Windows Forms applications. Why? If there are no list items bound to the binding manager, and you try to evaluate whether or not the Current propery is NULL, you'll get an IndexOutOfRangeException exception. That's because the Current property uses the binding manager's index value to retrieve the current list item. Since there are no items in the list, an exception is thrown. Below is an example of what I mean:

The wrong way: // C# Syntax BindingManagerBase bindingMgrBase = this.BindingContext[dataSet, "TBLMYTABLE"]; if (bindingMgrBase.Current != null) { // Do something here... } The right way: // C# Syntax BindingManagerBase bindingMgrBase = this.BindingContext[dataSet, "TBLMYTABLE"]; if (bindingMgrBase.Position > -1) { // Do something here... } Another right way: // C# Syntax BindingManagerBase bindingMgrBase = this.BindingContext[dataSet, "TBLMYTABLE"]; if (bindingMgrBase.Count > 0) { // Do something here... }


Comments Click Here to Add your Comment
No Comments Found.
 
COPYRIGHT/DISCLAIMER | CONTACT US