Yet another .addNew question

I'm doing the typical master-child form. Master's records are in textboxes, one checkbox. Child is in a grid. A relation joins the PK/FK.

MasterTable: SRProfile2
Child: SRProfile2Detail
PK/FK: Profile2Key
Relation: SRProfile2SRProfileDetail

I can do the following:
Delete a child
Add a child to an exising master
Edit a child

Can not 
Add a master + child.

Since i can do the 1st 3 items, I believe my databinding is correct. All control's datasource are datasets. 

In my form's load event I set the field for the check box as:
dsSR2.SRProfile2.Columns["Profile2AllDest"].DefaultValue = true;

My AddNew is here:
 private void btnNew_Click(object sender, System.EventArgs e)
        {
this.BindingContext[dsSR2, dsSR2.SRProfile2.TableName].EndCurrentEdit();
this.BindingContext[dsSR2, dsSR2.SRProfile2.TableName].AddNew();

// also tried this:                this.BindingContext[dsSR2, dsSR2.Relations["SRProfile2SRProfile2Detail"].RelationName].AddNew();
            }


The error I get whenI call AddNew against the context of my Master Table is

ForeignKeyConstraint SRProfile2SRProfile2Detail [this is a DataRelation]
requires the child key value (5264) to exist in the parent table. Do you
want to correct the value

The error I get when I call AddNew agaisnt the context of my Relation is 

"Cannot create a child list for field
SRProfile2SRProfile2Detail."

I"m using 1.1 and my Database is SQL Server 2000. I am using strongly typed DataSets.



Answer this question

Yet another .addNew question

  • Jian Zeng

    Simple

    when getting the bindingcontext (currencymanager) you must provide the correct relation and in the correct order.

    For example you have two tables.

    Table1
    ID
    Name

    Table2
    ID
    MyID
    Name

    Table2 is a child of table 1
    You create a relation called Relate1

    so to do an addnew to child table you would 

    me.bindingcontext(dataset1,"table1.relate1").addnew

    You can't just call relate, since you have to go though the parent first.

    You also want all your control bindings that bind to the child table to reflect this too. 

    So if the grid is using the child table it should be.

    datagrid1.datasource = mydataset
    datagrid1.datamember = "Table1.Relate1"

    and bindnig controls to a child you should do

    datase1.table1.relate1.columnname

    Hope this all helps

    Here is some more info
    http://www.syncfusion.com/FAQ/WinForms/default.asp#43

    also check out my sample at
    http://www.planet-source-code.com/vb/scripts/ShowCode.asp txtCodeId=2225&lngWId=10

  • dimaki

    Your bindings on your text boxes are wrong, I'm willing to bet they are bound to the table instead of the relation.

    You go to databindings, then the property then select the main table then the relation then the columns in the relation inside the main table. 

    You do this how ever deep you got to go depending on your structure.

  • Johnn manc

    Hi Joe.

    My datagrid already had these settings
    DataSource =  dsSR; 'dataset
    DataMember =  "SRProile2.SRProfile2SRProfile2Detail"; DataTable.DataRelation

    And I was calling this for BindingContext.AddNew:

    this.BindingContext[dsSR2, dsSR2.Relatons["SRProfile2SRProfile2Detail"].RelationName].AddNew();

    Now from your post, I changed that to:

    this.BC[dsSR2, "SRProfile2.SRProfile2SRProfile2Detail"].AddNew();

    Now when I run the code, the form adds a new row only to the DataGrid , the text fields that represent the main table do not change. 

  • Yet another .addNew question