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