Error when I both requery a related combobox and set its selectedvalue in code (on new records...


Error when I both requery a related combobox and set its selectedvalue...
Author
Message
Larry Tucker
Larry Tucker
StrataFrame User (173 reputation)StrataFrame User (173 reputation)StrataFrame User (173 reputation)StrataFrame User (173 reputation)StrataFrame User (173 reputation)StrataFrame User (173 reputation)StrataFrame User (173 reputation)StrataFrame User (173 reputation)StrataFrame User (173 reputation)
Group: StrataFrame Users
Posts: 69, Visits: 308
Hi all,

I'm stumped and have spent many hours puzzling over this.  I have two related comboxboxes, cboClient and cboCoverage on an EventBO maintenance form.   Each is bound to an FK on the EventBO record (CLI_ID and COV_ID). 

When the user selects a new Client, I want the cboCoverage to requery showing only the coverages for this particular client and I want to set its selected value in code to a particular one of the coverage choices.  The logic for this doesn't matter here (I am actually setting the recommended coverage for that client based on several other variables...); here is an ultra simplified version setting a fixed value in code just for demo:



 Private Sub cboClient_SelectionChangeCommitted(sender As Object, e As System.EventArgs)_
     Handles cboClient.SelectionChangeCommitted
        Me.cboCoverage.Requery()
        Me.cboCoverage.SelectedValue = 50022155  ' simplified for demo
 End Sub



This works fine when editing an existing record.  It fails on a new record with the following error:

System.InvalidCastException was unhandled by user code
  Message=Conversion from type 'DBNull' to type 'Integer' is not valid.
  Source=Microsoft.VisualBasic
  StackTrace:
       at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(Object Value)
       at ThomBillerBOLibrary.EventBO.FieldDescriptor.SetValue(Object component, Object value) in E:\ThomBiller\ThomBillerBOLibrary\ThomBillerBOLibrary\EventBO.Designer.vb:line 3003
       at System.Windows.Forms.BindToObject.SetValue(Object value)
       at System.Windows.Forms.Binding.PullData(Boolean reformat, Boolean force)
  InnerException:


Specifically, this is happening as the EventBO is trying to set the CLI_ID value based on the cboClient selection:


        Public Overrides Sub SetValue(ByVal component As Object, ByVal value As Object)
            Select Case Me.Field
                Case EventBOFieldNames.EVE_ID
                    DirectCast(component, EventBO).EVE_ID = CType(value, System.Int32)
                Case EventBOFieldNames.OLDPK
                    DirectCast(component, EventBO).OLDPK = CType(value, System.String)
                Case EventBOFieldNames.CLI_ID
                    DirectCast(component, EventBO).CLI_ID = CType(value, System.Int32)


What is so odd is that on a new EventBO record, I can either requery() the related cbo or set its selectedvalue without error, but not both.  If I am simply editing an existing EventBO record, I can do both.

TIA,

Larry
Tags
Replies
Larry Tucker
Larry Tucker
StrataFrame User (173 reputation)StrataFrame User (173 reputation)StrataFrame User (173 reputation)StrataFrame User (173 reputation)StrataFrame User (173 reputation)StrataFrame User (173 reputation)StrataFrame User (173 reputation)StrataFrame User (173 reputation)StrataFrame User (173 reputation)
Group: StrataFrame Users
Posts: 69, Visits: 308
So with Edhy's and Ivan's help, I've got it working.  Again, the problem was that when I tried to set combo2 only when the user made a selection in combo1, it was failing on a New record.  Using SelectedIndexChanged() helps, because it fires before the user has made a selection in combo1 and this somehow prevents the error.  So all I had to do was allow this to happen in an innocuous way as the form finished loading, then trap a true user selection.  To trap the user selection, I set a form property to hold the combo1's "before" value in its GotFocus(), then see if the "after" SelectedValue is different, indicating a user initiated change (not a simple navigation or form refresh()).

Here is the test code from the Strataframe Sample example.  Again, the actual linking of Orders and Products makes no sense, I am just using them as "combo1" and "combo2".


    Private Sub cboOrder_GotFocus(sender As Object, e As System.EventArgs) Handles cboOrder.GotFocus
        Me._BeforeOrderValue = CInt(cboOrder.SelectedValue) ' combo1 before value
    End Sub

    Private Sub cboOrder_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cboOrder.SelectedIndexChanged
        '-- Skip this event when the form is loading.
        If Me._FormIsLoading = True Then
            Exit Sub
        End If
        If OrderItemsBO1.Count > 0 Then
            Me.ProductsBO1.FillAll()        'combo2
            Me.cboProduct.Requery()         'combo2
            If Me.OrderItemsBO1.Count > 0 AndAlso Me.ProductsBO1.Count > 0 Then
                If CInt(cboOrder.SelectedValue) = _BeforeOrderValue Then
                    OrderItemsBO1.orit_prod_pk = 0 ' innocuous value on new record with no user interaction
                Else
                    OrderItemsBO1.orit_prod_pk = 2 ' desired value that would normally be based on something in combo1
                End If
            End If
        End If
    End Sub



This could probably be cleaned up.  Again, in my real situation, I want the suggested value for combo2 to be based on some business logic (in my case, the recommended insurance coverage for the selected client in combo1 based on a number of factors).  But here, at least, I am able to both requery() and set the value in combo2 based on user interaction in combo1.  That was the general problem and hopefully this is helpful to others.  It certainly was for me.  Thanks again Edhy and Ivan.

Larry
Ivan George Borges
Ivan George Borges
Strategic Support Team Member (4.9K reputation)Strategic Support Team Member (4.9K reputation)Strategic Support Team Member (4.9K reputation)Strategic Support Team Member (4.9K reputation)Strategic Support Team Member (4.9K reputation)Strategic Support Team Member (4.9K reputation)Strategic Support Team Member (4.9K reputation)Strategic Support Team Member (4.9K reputation)Strategic Support Team Member (4.9K reputation)
Group: StrataFrame MVPs
Posts: 1.9K, Visits: 21K
Glad you got it working, Larry. Wink
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Threaded View
Threaded View
Larry Tucker - 13 Years Ago
Edhy Rijo - 13 Years Ago
Larry Tucker - 13 Years Ago
Edhy Rijo - 13 Years Ago
Larry Tucker - 13 Years Ago
Larry Tucker - 13 Years Ago
Ivan George Borges - 13 Years Ago
Larry Tucker - 13 Years Ago
Ivan George Borges - 13 Years Ago
Edhy Rijo - 13 Years Ago
Larry Tucker - 13 Years Ago
Larry Tucker - 13 Years Ago
                 Glad you got it working, Larry. ;)
Ivan George Borges - 13 Years Ago

Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search