Welcome to Atlanta .NET Regular Guys Sign in | Join | Help

Help me out - data binding and custom business objects

Ok people, I need a hand with this .NET 2.0 code.

I've got a CBO to represent an bin (for putting stuff in) in my company's warehouse.  My bin object has 10 or so properties and I've got a collection of them which inherit from ArrayList so I can bind some combo boxes and list boxes to the collection of my CBO.  This works beautifully, although not as speedy as I'd hoped.

Here's the problem.  One of my CBO properties is an array of strings.  When I pull from the database, the value is a single row of space delimited characters.  I take this row and turn it into it's own arraylist turning:

"CD DV UV BC DO MK"

into

    CD
    DV
    UV
    BO
    EL
    MI

The problem is this.  I want to take these values and perform an additional database lookup to turn the array into this:

    CD | Compact Disk
    DV | DVD Video
    UV | Used Video
    BO | Books/Magazines
    EL | Electronics
    MI | Musical Instrument

So here's what my code looks like:
I've got a BinCollection object, a Bin object, and a BinConfig object.  My BinCollection object has a method, "GetValidBins" which executes a stored proc to retrieve the IDs of the bins I need to return to the user.  It iterates through these IDs, creating and adding Bins to the collection.  When a Bin is created (with a passed in ID), it's property values are initialized via a lookup to the DB.  When it goes to assign the value for it's configuration settings to the _alphaConfig property, it passes the string to a function which turns the space delimited string into an array of strings.  If I stop here, everything works.  My issue is that I need to bind a listbox to the _alphaConfig property when a user selects the Bin ID from a dropdown list.  So, instead of a simple array of strings, I've typed my _alphaConfig property as an ArrayList.  This also works, and I can bind my listbox...until...I try to perform an additional action on the values - appending the English description to the 2 character code.  Here is my code (VB.Net 2.0)

''' <summary>
''' Takes the DB formatted AlphCfg list of Alpha Configurations for bins and turns
''' it into a bindable arraylist with English descriptions.
'''
</summary>
''' <param name="alphaConfigs">a space delimited list of alphaConfigs (ex. CD DV EL MI)</param>
''' <returns>a bindable ArrayList of bin configs with English descriptions</returns>
''' <remarks></remarks>
Public Function DBListtoBindableList(ByVal alphaConfigs As String) As ArrayList
' break up the list of configs to get an array of individual configs
Dim myBindableConfigs As ArrayList = StringHandlers.ReturnList(alphaConfigs)
' ' connect to the database
' Dim db As Database = DatabaseFactory.CreateDatabase    ' yes, I'm using the EnterpriseLibrary v 2.0
' Dim i As Integer = 0
' For Each config As String In myBindableConfigs 
'    Dim sqlCommand As String = StringHandlers.StringRecompost(SQLStatements.SELECTSINGLEALPHACONFIG, config)  ' builds my select statement
'    Dim dbCommand As DbCommand = db.GetSqlStringCommand(sqlCommand)
'    Dim dbResult As DataSet = db.ExecuteDataSet(dbCommand)
'    For Each row As DataRow In dbResult.Tables(0).Rows
'       myBindableConfigs.Item(i) = row.Item(0).ToString
'       i += 1
'    Next
' Next

Return myBindableConfigs
End Function

 

If I uncomment the code above, I get InvalidOperationExceptions every time I try to make a change to the ArrayList value and it tells me that I'm breaking it's ability to be bindable. If I catch and handle the InvalidOperationExceptions (to keep the program running) I find that I have, indeed, broken the bindability of my list (but it does have the values I wanted).

Help!

-- Matt Ranlett

Published 10-01-2006 08:48 by Matt Ranlett
Filed Under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Matt Ranlett said:

Chris Wallace of http://www.uallas.com provided a working solution - just use a new array instead of editing the original one.
January 10, 2006 7:15 AM
 

Jim Wooley said:

As an alternative to using a new array, you may want to consider replacing the array with a NameValueCollection or, depending on your needs, a List<CustomLookup> where the CustomLookup class has a Key and Description property. If you concatenate the key and description (as it appears you are doing from the sample code), you won't get the key back to the BO. Instead, keep them separate and set the DisplayMember and ValueMember of the ComboBox as necessary.

As an aside, watch out for nulls returned from the SelectedItem/SelectedValue of the comboBox to your property set of the BO. Even if you designate the property to be of type String (not nullable string), the value in the property set may be Null and thus create unexpected results. The issue appears typically if trying to clear a previously selected value. Also, de-selecting a value does not fire SelectedItemChanged, thus binding to that event can be dangerous.
January 10, 2006 7:40 AM

What do you think?

(required) 
(optional)
(required) 

About Matt Ranlett

One of the two original Atlanta .NET Regular Guys, Matt fills his free time by helping to run several Atlanta area user groups, the Atlanta Code Camps, and works as one of the two INETA co-Vice Presidents of Technology
SkinName:iroha_Blog2
Powered by Community Server, by Telligent Systems