Count of BO is Zero(0)


Author
Message
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.3K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Great! Glad that helped!
Terry Bottorff
Terry Bottorff
Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)
Group: Forum Members
Posts: 448, Visits: 12K
That worked perfect. Thank you so much.
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.3K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
I think the only way this occurs is if the BO is on a SF StandardForm. The form handles the delete message. There is a property to turn this off on the form. See this post:



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



The form handles this for any BO that is in its BO collection. I believe if you do this via code only (i.e. instantiate the BO in code), there will be no messages.
Terry Bottorff
Terry Bottorff
Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)
Group: Forum Members
Posts: 448, Visits: 12K
Greg thank you so much. I sort of had the right idea but The Order I did things Sucked. But what you gave me made sense and worked great.

One last question on this. Is there a way to keep from having to answer yes every time you deletecurrentrow?

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.3K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Terry,



Not sure I'm following your logic, so it is hard to offer detailed help. However, maybe I can offer some bits of help that will keep you moving.



First, this paradigm really isn't going to do anything:



SwStockBO1.Clear()

SwStockBO1.Save()




Nothing will ever get saved. And if there were changes in the SwStockBO1, they'd be lost. Clear() removes all the rows in the underlying DataTable. Those rows drive if there is anything to save or not. A DataRow has a row state that indicates if it is dirty and how it is dirty (new, edited, deleted). When a Save() happens, it walks the rows and either adds, updates or deletes the rows. No rows to walk, nothing to do.



Because of this, the first part of your code never does anything in the database, thus the second part (were you add the new rows) fails, because those first four records are never deleted.



So, for the first part if you did something like:



MessageBox.Show(SwStockBO1.Count.ToString, "Number in Stock BO")

Dim narray(4) As Integer

SwStockBO1.MoveFirst()

For i = 0 To 4

  narray(i) = SwStockBO1.stock

  SwStockBO1.DeleteCurrentRow(True)

  SwStockBO1.MoveNext()

Next

MessageBox.Show(SwStockBO1.Count.ToString, "Number in Stock BO")

SwStockBO1.Save()




You'd walk the first four rows of the BO, cache the "stock" value in your narray, mark those four rows for deletion, then actually delete those rows from the database when Save is called (and they'd be removed from the BO then as well).



If you just want to loop through the entire BO and delete every thing, you could also do:



MessageBox.Show(SwStockBO1.Count.ToString, "Number in Stock BO")

Dim narray() As Integer

Dim stockCount As Integer = SwStockBO1.Count

Redim narray(stockCount -1)

SwStockBO1.MoveFirst()

For i As Integer = 0 To stockCount -1

  narray(i) = SwStockBO1.stock

  SwStockBO1.DeleteCurrentRow(True)

  SwStockBO1.MoveNext()

Next

' This will report number of records in BO (nothing removed yet)

MessageBox.Show(SwStockBO1.Count.ToString, "Number in Stock BO")

SwStockBO1.Save()

' After save, the BO will be empty and count should be zero

MessageBox.Show(SwStockBO1.Count.ToString, "Number in Stock BO")




Here I'm just using the count of the BO to drive the size of the array and the loop.



I just typed this in, so there might be some errors, but hopefully it helps you understand what is going on and gets you moving! Good luck!



Terry Bottorff
Terry Bottorff
Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)
Group: Forum Members
Posts: 448, Visits: 12K

MessageBox.Show(SwStockBO1.Count.ToString, "Number in Stock")

Dim narray(4) As Integer

SwStockBO1.MoveFirst()

For i = 0 To 4

narray(i) = SwStockBO1.stock

SwStockBO1.MoveNext()

Next

SwStockBO1.Clear()

MessageBox.Show(SwStockBO1.Count.ToString, "Number in Stock")

SwStockBO1.Save()

narray(2) = 0

Dim ncnt As Integer = 1

For i = 0 To 4

If narray(i) <> 0 Then

SwStockBO1.NewRow()

SwStockBO1.swst_pk = ncnt

SwStockBO1.stock = narray(i)

ncnt += 1

End If

Next

MessageBox.Show(SwStockBO1.Count.ToString, "Number in Stock")

SwStockBO1.Save()

The first messagebox indiates 5 records which is what is there, the second indicates 0 which is what I would suspect and the third is 4 which is what I want.

Alright Alex, Edhy and Greg what am I missing here? Why when the above code in the simple Click of a Button is executed don't I get 4 records in the table instead of an error saying can not have duplicate Primary Keys. I am doing my own primary key right now. Even if I change the primary key to one that is not found in the table I then get 9 records instead of the 4 I want.

Do I need a SStockBO1.DeleteCurrentRow() instead of just a clear and save? Well I inserted this code

Do While SwStockBO1.Count > 0

SwStockBO1.DeleteCurrentRow()

Loop

and of course it asked me about deleting each record(which I can not have in real life because there would be too many records) but I still get the same result? TIA

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.3K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Terry,



I'm not sure if this will help, but the BO is disconnected from the data. Thus, when you fill it, you lookup data in the db and load it into the BO. Once that is done, the BO isn't talking to the db any longer (until the BO needs to talk to it again, for another fill, to save or delete data). Thus, the Clear() method of the BO simply clears any data that was loaded into it, which has NO effect on the actual data (it's disconnected from it). This is great if you need to "smoke" (a bit of Texan vocabulary I picked up from Trent) the data in the BO in order to load another set of data into it.



However, if what you want is to actually delete data from the db, you need to go about it a bit differently. There are a couple of possibilities, depending on what you want to do.



First, if you already have the data loaded into the BO and you want to delete some or all of that data, you would add a method to the BO to loop through the rows of the BO and call Delete() on each row you want to delete. You would use a For loop and use the .Count property of the BO. Probably the most efficient way is to mark the rows for deletion and then do a Save() to actually delete them.



The second way is if you just need to delete a set of rows based on some criterion, like smokin all the records older than a certain date. In this case you would write a method in the BO that would accept the necessary parameters and either build some SQL or call a sproc to just execute a DELETE statement in the database.



I hope that clears up what some of your options might be in order to delete data in the db vs. clearing loaded data from a BO.
Edhy Rijo
E
StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Hi Terry,

There must be something in the way you are checking the BO count.  Do the following test:

  • Fill your BO with some records
  • Test your BO.Count it should have the amount of records you filled with.
  • Them do a BO.Clear, it should reset the BO.Count = 0

The pk's are automatically generated. I know I used 8 and 100, how do I write the remaining stock back? TIA.

If I understand you correctly, you could have several records in your BO and out of those you just want to save a couple, if this is right, you can do this several ways:

  • Have a flag field which will allow you to filter out the records you want to save, then use the BO.Filter = "MyFlagField = True" or something along those lines.
  • Or you can iterate through all the BO records, copy the ones you want to save into another BO, then save the other BO.

If that was not what you meant, please let us know.

Edhy Rijo

Terry Bottorff
Terry Bottorff
Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)Advanced StrataFrame User (770 reputation)
Group: Forum Members
Posts: 448, Visits: 12K
These statements did not get rid of the 100 original records in the table.

Bo.clear()

Bo.Save()

I think there must be a way to get rid of the original 100 records with out an iteration thru the records but I have not found it yet. Thanks.

While I am here lets say Table A has the following Records(pk=primary key):

pk=1 stock=5

pk=2 stock=8

pk=3 stock=100

pk=4 stock=200

Suppose we use 8 and 100 and I want to write the remaining stock back to the table so the table contains:

pk=5 stock=5

pk=6 stock=200

The pk's are automagically generated. I know I used 8 and 100, how do I write the remaining stock back? TIA.

Edhy Rijo
E
StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Hi Alex,

Thanks for the kind comments, I also learned a lot from others here and continue to do so everyday.

Wouldn't the .Clear( ) only operate on the rows the BO retrieved?

Yes, basically based on the comment in the SF source code, the BO.Clear method will do reset the data table with no records.

''' Clears all data in the business object and sets the current data table back to its initial state

''' with all columns belonging to the table schema



Edhy Rijo

GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search