StrataFrame Forum

Add a new table/row programmatically

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

By Gerhard Jaros - 10/16/2006

I've a dataset and want to add this data to an existing BusinessObject. The fields of the business object are exactly the same than those of my data table. How can I automatically add this data? BO.NewRow() just adds an empty row and also BO.Add()

...gerhard...

By Trent L. Taylor - 10/16/2006

All business objects have a CopyDataFrom method that allows you to copy data into it.  You can copy data from another BO or a DataTable:

MyBO.CopyDataFrom(YourDataTable, ClearAndFillFromDefaultValue)

By Gerhard Jaros - 10/16/2006

What could be the reason that myBO.CopyDataFrom() does nothing?
By Trent L. Taylor - 10/16/2006

You will have to give a little more detail for me to answer that.  What does your call to CopyDataFrom look like and what data is in you data table.
By Gerhard Jaros - 10/30/2006

The source code is:

this.CopyDataFrom(ds.Tables[0], BusinessCloneDataType.ClearAndFillFromCompleteTable);

this.Save();

 

In my ds.Tables[0] there are 11 rows, but after executing these commands there are just 11 empty rows in my BO

By Gerhard Jaros - 10/30/2006

Sorry, it doesn't append 11 empty rows, it appends nothing.

...gerhard...

By Trent L. Taylor - 10/30/2006

Do you have the same structure in the data table that is populated within the BO?  If you have called a Fill() command or popualted the BO and then load a data table that does not have the same structure, you are going to have some trouble.
By Gerhard Jaros - 10/30/2006

The structure is exactly the same
By Gerhard Jaros - 10/30/2006

There are two things which could maybe help me:

1. Where can I find the source code of CopyDataFrom() in the Source Code Directory?
2. If this would be an SQL error, where could I find the SQL error message?

By Trent L. Taylor - 10/30/2006

There is not a lot of rocket-science in this method BigGrin  If you debug this code and look at it in the watch window (your data table and the BO.CurrentDataTable) before and after the CopyDataFrom method, do you not see the records?
By Trent L. Taylor - 10/30/2006

Where can I find the source code of CopyDataFrom() in the Source Code Directory?

This will be in the BusinessLayer class found in the MicroFour StrataFrame Business project when you open the source solution.

If this would be an SQL error, where could I find the SQL error message

I think your problem is happening before you ever get here.  You need to look at the data table in the debugger to see if the data gets copied over before you call the save command.

By Gerhard Jaros - 10/30/2006

Smile

this.CurrentDataTable is empty before I run CopyDataFrom and it contains all my data after CopyDataFrom().

By Gerhard Jaros - 10/30/2006

Smile

this.CurrentDataTable is empty before I run CopyDataTable() and it contains all my data after CopyDataTable()

By Trent L. Taylor - 10/30/2006

Then you need to either put a debug on your data connection in the program.cs file at the bottom of the SetDataSources() method:

MicroFour.StrataFrame.Data.Databasics.DataSources(0).SetDebugOn("c:\\temp\\mydebug.html",True);

or you need to capture the ErrorSaving event of the BO and look at the event args to see if you are getting an error. 

Last, what does the Save() method return as a result.  You can use all of these to figure out where your problem is.

By Gerhard Jaros - 10/30/2006

Trent L. Taylor (10/30/2006)
Then you need to either put a debug on your data connection in the program.cs file at the bottom of the SetDataSources() method:

MicroFour.StrataFrame.Data.Databasics.DataSources(0).SetDebugOn("c:\\temp\\mydebug.html",True);.

mydebug.html is empty.

or you need to capture the ErrorSaving event of the BO and look at the event args to see if you are getting an error. 

I cannot understand what you mean with "capture the errorSaving event"

Last, what does the Save() method return as a result.  You can use all of these to figure out where your problem is.

Save returns "Success"

By Trent L. Taylor - 10/30/2006

mydebug.html is empty.

Not possible if you have the correct data source mapped.  This is not accessible until you close down your application.  You have something wired improperly it sounds like....there are at least a dozen or more things that could be going on...all of which point to something related to your data connection.  How are you setting up your data connection?

By Gerhard Jaros - 10/30/2006

Nobody on this world can ever say that you're not patient Smile

I did nothing unusual when I set up my data connection:

private static void SetDataSources()

{

// The application key:

ConnectionManager.ApplicationKey = "ChangeThisValue";

ConnectionManager.ApplicationDefaultTitle = "EKZ7 Connection";

ConnectionManager.ApplicationDefaultDescription = "This application connection is used by EKZ7";

// SQL Connection

ConnectionManager.AddRequiredDataSourceItem("", "SQL Connection",

DataSourceTypeOptions.SqlServer, "EKZ7", "This connection is used by EKZ7.");

ConnectionManager.SetConnections();

MicroFour.StrataFrame.Data.DataBasics.DataSources[0].SetDebugOn(@"c:\temp\myDebug.html", true);

}

By Trent L. Taylor - 10/30/2006

This looks OK....did you try to Save() your changes with this code in place?  Nothing is written to this file until a SELECT, DELETE, UPDATE, or INSERT is called.
By Gerhard Jaros - 10/30/2006

Yes, I use this lines of code since I work on this project and I'm quite sure that it works pretty well because it works well in lots of other parts of my app.

It really seems as if the Save() method would simply do nothing. All the data is in the CurrentDataTable() and Save() does absolutely nothing. It even does not change it's own status "Success". If I work with another part of my application, the myDebug.html file gets content. It's JUST in this point of my app.

By Trent L. Taylor - 10/30/2006

This would explain all of your other problems.  Do you have anything in the DataSourceKey property?  There is something related to this BO that is not executing the Save().  Since nothing shows up in this file, this tells us that it is not trying to use this data source connection.
By Gerhard Jaros - 10/30/2006

There is nothing in my DataSourceKey property, and if I insert data manually (with my StrataFrame form where I use the same BO) it works perfectly Smile It just doesn't work if I call the Save() method programmatically.
By Trent L. Taylor - 10/30/2006

I bet your BO is not dirty when you are copying over the data.  Look at the IsDirty property after you call the CopyDataFrom method. 

MyBO.IsDirty

If it is false, then this is your problem.  You need to place the BO in an edit state before copying the data into it. 

Basically, all ADO.NET data rows have a RowState that indicates whether or not the row is dirty and needs to be saved.  More than likely your data table has already accepted the changes which will not force a save.  You can force each row to dirty by doing the following:

loBO.CopyDataFrom(MyDataTable, MicroFour.StrataFrame.Business.BusinessCloneDataType.ClearAndFillFromDefaultView)

If loBO.MoveFirst() Then
    Do
        '-- Force a new record to be created when saved
        loBO.CurrentRow.SetAdded()
    Loop While loBO.MoveNext()
End If

loBO.Save()

By Gerhard Jaros - 10/30/2006

Trent L. Taylor (10/30/2006)
I bet your BO is not dirty when you are copying over the data. 

You won the bet BigGrin

The Copy command works fine now, there just happend one simple thing which you could maybe tell me: I have a myBO.Clear() command in the same procedure - why does this one not set myBO.IsDirty to true? Right now all the existing rows are kept and the new rows coming in from CopyTableFrom are added, and the reason is that

myBO.Clear();
myBO.Save();

doesn't work because the isDirty prop is set to false. And I couldn't find a SetDeleted() method (like the SetAdded() method before).

Thanks for your help!

By Gerhard Jaros - 11/2/2006

Do you maybe have an answer to my question? I meanwhile tried all kinds of different delete events and they also do not set the isDirty prop to true.

Thank you very much!

...gerhard...

By Trent L. Taylor - 11/2/2006

I don't know what you are trying to accomplish.  The Clear removes any of the data within the BO and resets any flags.  Calling a Clear() and then a Save() would do literally nothing.  What are you trying to accomplish?

Just so you can better understand this, run this code:

MyBO.Clear()

MsgBox(MyBO.Count.ToString())

You will get a message box with a "0" in it.  There are no records in the BO after you call a clear...the records are not deleted, the BO is just cleared of its data.  If you want to delete all of the records in the BO, the easiest thing to do is enumerate and get all of the PKs then delete all of the PKs at once:

Dim loPKs As New System.Collections.Generics.List(Of Integer)

If MyBO.MoveFirst()
    Do
         loPKs.Add(MyBO.MyPrimaryKey)
    Loop While MyBO.MoveNext()
End If

'-- Now delete the records
For Each lnPK As Integer IN laPKs.ToArray()
    MyBO.DeleteByPrimaryKey(lnPK)
Next

You don't need to call the save because the DeleteByPrimaryKey immediately deletes the record on the server.