StrataFrame Forum

error when BO is empty

http://forum.strataframe.net/Topic24465.aspx

By Felix M Avendano - 9/1/2009

I have a form with 2 comboboxes, one datagrid, 3 BO's and one BBS. The Bo's are related between them in a father, child, grandchild relationship. The BBS is attached to the datagrid. The datagrid receives the data of the grandchild.

I have the code that follows in the form:

Public Class frmTrabajoseinsumos


    Private Sub ComboBox2_ListPopulating(ByVal e As MicroFour.StrataFrame.UI.ListPopulatingEventArgs) Handles ComboBox2.ListPopulating
        If Me.BoEmpresa1.Count > 0 Then
            e.Parameters(0).Value = CInt(ComboBox1.SelectedValue)
        Else
            e.Parameters(0).Value = 0
        End If

    End Sub

    Private Sub ComboBox1_ParentFormLoading() Handles ComboBox1.ParentFormLoading
        Me.BoEmpresa1.FillAll()
    End Sub


    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        If (CInt(ComboBox1.SelectedValue)) > 0 Then
            BoTipotrabajoinsumo1.FillByCustomPK(CInt(ComboBox1.SelectedValue))
            ComboBox2.Requery()

        End If
        If Me.BoTrabajoinsumo1.Count > 0 Then
            BoTrabajoinsumo1.FillByCustomPK(CInt(ComboBox1.SelectedValue), CInt(ComboBox2.SelectedValue))
        End If
    End Sub


    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        'If Me.BoTrabajoinsumo1.Count > 0 Then
        BoTrabajoinsumo1.FillByCustomPK(CInt(ComboBox1.SelectedValue), CInt(ComboBox2.SelectedValue))
        DataGridView1.Refresh()


    End Sub

End Class

The problem cames in the last ComboBox2_SelectedIndexChanged. When the query of the FillByCustomPK cames with no data and the BO is empty I get the following error:

The CurrentRow for table '[dbo].[Trabajoinsumo]' could not be evaluated because the CurrentRowIndex is out of range.  Business object record count: 0.  CurrentRowIndex: -1.

The error is launched in a part of the generated code:

''' <summary>
    ''' Tipoactividad
    ''' </summary>
    <Browsable(False), _
     BusinessFieldDisplayInEditor(), _
     Description("Tipoactividad"), _
     DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
    Public Property [Tipoactividad]() As System.Int32
        Get
            Return CType(Me.CurrentRow.Item("Tipoactividad"), System.Int32)
        End Get
        Set(ByVal value As System.Int32)
            Me.CurrentRow.Item("Tipoactividad") = value
        End Set
    End Property

Ive being trying to do my best but I cant find a solution in order to avoid the error, unless I change the generated code, and that's awfol.

Any Sugestion wil be well received.

By Edhy Rijo - 9/2/2009

Felix,



The rule is that whenever you will be accesing the BO property, you must check for the BO.Count like you are doing for the other combos.



Also, instead of using the combo.SelectedValue, use the binded property of the combo:



e.Parameters(0).Value = BO.PropertyName)

By Felix M Avendano - 9/2/2009

Edhy Rijo (09/02/2009)
Felix,

The rule is that whenever you will be accesing the BO property, you must check for the BO.Count like you are doing for the other combos.

OK to this, I'll be more carefull.w00t


Also, instead of using the combo.SelectedValue, use the binded property of the combo:

e.Parameters(0).Value = BO.PropertyName)

This is the one I'm more interested on. Thanks for the suggestion.Smile

Regards

By Felix M Avendano - 9/2/2009

Ive change everything as suggested and now I have 2 Comboboxes bounded to a cuple od BO's and a datagridview bounded to a BBS and this bbs to a BO. Everything works fine intil I try to exit the form. When I go out of it thru the close form icon I get this error. Any Clue, because I cant debug it because it doesn't pass thru my code.

KeyNotFoundException
  La clave proporcionada no se encontró en el diccionario. (the key provided is not found in the diccionary)

Source     : mscorlib

Stack Trace:
   en System.ThrowHelper.ThrowKeyNotFoundException()
   en System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   en MicroFour.StrataFrame.Business.BusinessLayer.get__CurrentDataTable(Boolean IsSharedTable)
   en MicroFour.StrataFrame.Business.BusinessLayer.get_Count()
   en MicroFour.StrataFrame.Business.BusinessBindingSource.get_IBindingListView_Count()
   en System.Windows.Forms.CurrencyManager.get_Count()
   en System.Windows.Forms.DataGridViewCell.GetValue(Int32 rowIndex)
   en System.Windows.Forms.DataGridViewCell.GetFormattedValue(Int32 rowIndex, DataGridViewCellStyle& cellStyle, DataGridViewDataErrorContexts context)
   en System.Windows.Forms.DataGridViewTextBoxCell.GetPreferredSize(Graphics graphics, DataGridViewCellStyle cellStyle, Int32 rowIndex, Size constraintSize)
   en System.Windows.Forms.DataGridViewCell.GetPreferredWidth(Int32 rowIndex, Int32 height)
   en System.Windows.Forms.DataGridViewCell.OnCellDataAreaMouseEnterInternal(Int32 rowIndex)
   en System.Windows.Forms.DataGridViewCell.OnMouseMoveInternal(DataGridViewCellMouseEventArgs e)
   en System.Windows.Forms.DataGridView.OnCellMouseMove(DataGridViewCellMouseEventArgs e)
   en System.Windows.Forms.DataGridView.UpdateMouseEnteredCell(HitTestInfo hti, MouseEventArgs e)
   en System.Windows.Forms.DataGridView.OnMouseMove(MouseEventArgs e)
   en System.Windows.Forms.Control.WmMouseMove(Message& m)
   en System.Windows.Forms.Control.WndProc(Message& m)
   en System.Windows.Forms.DataGridView.WndProc(Message& m)
   en System.Windows.Forms.Control.ControlNativewindow.OnMessage(Message& m)
   en System.Windows.Forms.Control.ControlNativewindow.WndProc(Message& m)
   en System.Windows.Forms.Nativewindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

By Edhy Rijo - 9/2/2009

That problem was fixed in the latest version 1.7.0.2, download it from your account and you should be good.
By Trent L. Taylor - 9/2/2009

Yup, please download the most recent build and see if your problem persists. Thanks.
By Felix M Avendano - 9/3/2009

It did work fine, but I still have a problem.

I tried to simplyfied it the best I could in one form because I have this in 3 forms and I cannot solve it. Let's see.

I have a Datagridview bounded via a BBS to a first BO. The Data... has a combobox inthere bounded to a second BO via a second BBS. I populate evreything as well and when I try to add a record using the datagrid directly, or via a Button with the add function is the same, I have a very ugly message, but I cannot debug it, or I don't know how to do it.

Ill attach you the message window.

By Felix M Avendano - 9/4/2009

I've finally found where the error is:

at windowsaplication1.BOempresa  (is a BO of course Smile)

       Public Overrides Function GetValue(ByVal component As Object) As Object
            Select Case Me.Field
                Case BOEmpresaFieldNames.Descripcion
                    Return DirectCast(component, BOEmpresa).Descripcion
                Case BOEmpresaFieldNames.CUIT
                    Return DirectCast(component, BOEmpresa).CUIT
                Case BOEmpresaFieldNames.Dirección
                    Return DirectCast(component, BOEmpresa).Dirección
                Case BOEmpresaFieldNames.Ubicacion
                    Return DirectCast(component, BOEmpresa).Ubicacion
                Case BOEmpresaFieldNames.Empresa  (after passing 4 times here)
                    Return DirectCast(component, BOEmpresa).Empresa
                Case Else
                    Throw New BusinessLayerException("Field not supported.")
            End Select
        End Function

By Felix M Avendano - 9/4/2009

Now I have something good!!!

If I put Empresa as parent key works fine. The problem is, what about having two or more foreing keys. I cannot put more than one parent.

By Trent L. Taylor - 9/4/2009

So what you are saying is that you need to register more than one foreign key for the BO, correct? You can use the RegisterForeignKey method on the BO and pass over the child BO and the key information. This must be done in code, but will server the exact same purpose as the standard ParentRelationship property available through the designer.
By Felix M Avendano - 9/8/2009

I´ve was working around with this but I´m not getting the same result as using the properties. So here are some questions. Supouse Ive got BOParent and BOChild.

    Private Sub BOChild_ParentFormLoading() Handles BoEmpresa1.ParentFormLoading
        ...

        BOParent.RegisterForeignKey(BOchild, "ChildFKFieldname")

....
    End Sub

By Felix M Avendano - 9/8/2009

I was working around with this and there ares somethings I ´m not sure. Actually it does´t work as I espect.

Sopouse I´ve got a BOparent and a BOChild

    Private Sub BoParent_ParentFormLoading() Handles BoParent1.ParentFormLoading
        ...
        BoParent1.RegisterForeignKey(BOchild, "ChildFKFieldname")

       ...

    End Sub

Is this correct. Or I have to put this in the BO instead of the instance of it?

I have to use the method of the parent or the child's one.

That's allWink

Regards

and, the method is correct? or there is something better.

By Trent L. Taylor - 9/9/2009

It looks as though you are going about this correctly, but the code snippet you gave doesn't show the whole picture.  But in short, if your foreign keys are getting updated, then you are in good shape.