StrataFrame Forum

Saving Data and Getting First and Last Records?

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

By Terry Bottorff - 8/17/2010

I was just trying this simple code and notice in attachment that the only check numbers that got saved are the first and last ones???

I know that the code may not be the best but I could not get getenumerabe to work so I was trying this way.





' Now Add the Check Numbers

If RbStockChecksBO1.MoveFirst Then

For i = 1 To RbStockChecksBO1.Count

Me.RbStockChecksBO1.checknum = nchk

nchk += 1

Me.RbStockChecksBO1.MoveNext()

Next

Me.RbStockChecksBO1.Save()

End If





What am I overlooking? TIA.
By Edhy Rijo - 8/17/2010

Hi Terry,



Here is the GetEnumerable() version, see if that work for you:



For Each record As RbStockChecksBO In Me.RbStockChecksBO1.GetEnumerable()

record.checknum = nchk

nchk += 1

Next

If Me.RbStockChecksBO1.IsDirty() Then

Me.RbStockChecksBO1.Save()

End If

By Terry Bottorff - 8/18/2010

Edhy now I get only the first record saved with this code? See the attached figure.





For Each rec As RBStockChecksBO In Me.RbStockChecksBO1.GetEnumerable()

rec.checknum = nchk

nchk += 1

Next

If Me.RbStockChecksBO1.IsDirty() Then

Me.RbStockChecksBO1.Save()

End If





This should not be this hard? TIA.
By Terry Bottorff - 8/18/2010

I put the messagebox in the code for my own sanity and by the attached picture you can see what I got.



For Each rec As RBStockChecksBO In Me.RbStockChecksBO1.GetEnumerable()

rec.checknum = nchk

nchk += 1

Next

If Me.RbStockChecksBO1.IsDirty() Then

Me.RbStockChecksBO1.Save()

End If





I traced the above code and it loop One (1) time thru??????? Thus saving one check number as prior post shows.??? w00t

Very bazzar.
By Edhy Rijo - 8/18/2010

Are you using some filter or sort in the BO? if so, then reset those before the loop and put them back after the loop.



Something like this:



Dim currentFilter as string = MyBO.Filter

... do FOR..NEXT here

MyBO.Filter = currentFilter





If using BO.Sort, do the same.
By Terry Bottorff - 8/18/2010

None that I put on. I did fill the BO with an 'order by' in the stored procedure that I used to fill the BO. I know that should not count.

UG.
By Terry Bottorff - 8/19/2010

I found a way around it by executing an ExecuteScalar and all seems to be good. I still don't have a clue as to why it worked the way it was. Oh well maybe a Window's Thing, or a SQL Thing or .... Thing...?
By Edhy Rijo - 8/19/2010

Hi Terry,



Sorry for delay in getting back to you!



That is very weird, something else may be happening with your whole code, I used BO.GetEnumerable() all over since Trent introduced it to us. If you still want to give this another try, please post the whole method code or email it to me so I can take a wider look.Cool
By Terry Bottorff - 8/20/2010

Thank you for the extreme amount of help you have given me. I will try and get back to this later and see what is happening.

I do have a question though. If you have a simple table that is say (tableA) and thus a BO with the following structure:

PK as primary key, Name, amount,

and you fill TableA with with say: me.TableABO.filldatatable("Select name, amount from TableB")

Can you put something in the Select so that the PK column will exist and not cause problems when you try and save tableA's BO?

If you don't have something there it won't let you save because the BO does not have a Primary Key and if I put save 1 in for all the records and the PK is autoincrementing I get an error?




By Edhy Rijo - 8/20/2010

Terry Bottorff (08/20/2010)
Thank you for the extreme amount of help you have given me. I will try and get back to this later and see what is happening.

I do have a question though. If you have a simple table that is say (tableA) and thus a BO with the following structure:

PK as primary key, Name, amount,

and you fill TableA with with say: me.TableABO.filldatatable("Select name, amount from TableB")

Can you put something in the Select so that the PK column will exist and not cause problems when you try and save tableA's BO?

If you don't have something there it won't let you save because the BO does not have a Primary Key and if I put save 1 in for all the records and the PK is autoincrementing I get an error?





Hi Terry,



If you plan to update the table via the BO you MUST have the PK column as part of your select statement so it will know which record to update.

me.TableABO.filldatatable("Select PK, name, amount from TableB")




StrataFrame Business Objects are very, very, very flexible and that make them powerful. One sample I used a lot is having a JOIN statement to select a field description, then in the BO I have a Custom Field Property that will return the column name for the description field, of course your SQL statement should have that column as part of it. Here is a sample of one of my Custom Field Properties:



Protected Overrides Function GetCustomBindablePropertyDescriptors() As FieldPropertyDescriptor()

' Create and return a new array of FieldPropertyDescriptor objects that contains

' the ReflectionPropertyDescriptor for the Custom Fields.

Return New FieldPropertyDescriptor() _

{New ReflectionPropertyDescriptor("cfp_CustomerName", GetType(bizTransactionItems))}

End Function




BusinessFieldDisplayInEditor(), _

Description("Customer Name"), _

DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _

Public Overridable Property [cfp_CustomerName]() As String

' Check to see if datatable contains the custom field in

' case object was filled by a method that doesn't contain the field.

Get

Dim columnName As String = "cfp_CustomerName"

If Me.CurrentDataTable.Columns.Contains(columnName) Then

If Not TypeOf (Me.CurrentRow.Item(columnName)) Is DBNull Then

Return CStr(Me.CurrentRow.Item(columnName))

Else

Return String.Empty

End If

Else

Return String.Empty

End If

End Get

Set(ByVal value As String)

Dim columnName As String = "cfp_CustomerName"

If Me.CurrentDataTable.Columns.Contains(columnName) Then

Me.CurrentRow.Item(columnName) = value

End If

End Set

End Property





Like I said, the field column "cfp_CustomerName" should exist in the SQL statement used to fill the BO, if not, the used of the Custom Field Property BO.cfp_CustomerName will just return a blank space. There are a lot of samples in the forums on how to use the Custom Field Properties as well as in the SF help file. I must admit that the above code is a combination of multiple SF developers suggestion in the forums, so it is not my idea BigGrin



P.S.

Copy and paste the code in VS so it will make more sense. Hehe
By Terry Bottorff - 8/20/2010

OK, Thanks I will put some time on this.