LINQDataSource / FormView - automatically updating child records

Discussion in 'ASP .Net' started by Jay Pondy, Apr 4, 2008.

  1. Jay Pondy

    Jay Pondy Guest

    I have a CheckBoxList control on a FormView.

    Each item in the CheckBoxList control is a child record of the parent
    displayed in the formview.

    I am trying to update the child records of the parent in the LINQDataSource
    Updating Event as follows:

    protected void dsNearMissIncident_Updating(object sender,
    LinqDataSourceUpdateEventArgs e)
    {

    // Get the Parent Record
    var oIncident = (Incident)e.NewObject;

    // Each item in the checkboxlist is a child of the parent
    CheckBoxList chkStates = (CheckBoxList)fv.FindControl("chkStates");

    // Loop through the CheckBoxList items and figure out which ones need to
    be added/removed as children
    foreach (ListItem oItem in chkStates.Items)
    {

    var oState = new NearMissSafeStartState { StatePKID =
    int.Parse(oItem.Value), IncidentPKID = oIncident.PKID };

    // Item is selected and not present - add it
    if (oItem.Selected &&
    !oIncident.NearMissSafeStartStates.Contains(oState))
    oIncident.NearMissSafeStartStates.Add(oState);

    // Item is de-selected and is present - remove it
    else if (!oItem.Selected &&
    oIncident.NearMissSafeStartStates.Contains(oState))
    oIncident.NearMissSafeStartStates.Remove(oState);
    }
    }

    What I would expect to see is the LINQDataSource automatically update the
    child records but it does not, only the parent sees any changes - it's as if
    the child records are not even there.

    If I hand code creating a new parent and adding children as follows the
    children ARE automatically updated:

    NearMissDataContext dc = new NearMissDataContext();
    Incident oIncident = new Incident { DepartmentPKID = 28, Description =
    "Description", InjuryPotentialPKID = 1, Occurred = System.DateTime.Now,
    SubmittedByPKID = 1169, SupervisorPKID = 1169, TOPReviewerPKID = 1169 };

    oIncident.NearMissSafeStartErrors.AddRange(new NearMissSafeStartError[] {
    new NearMissSafeStartError { ErrorPKID = 1 } ,
    new NearMissSafeStartError { ErrorPKID = 2 },
    new NearMissSafeStartError { ErrorPKID = 3 },
    new NearMissSafeStartError { ErrorPKID = 4 }});

    oIncident.NearMissSafeStartStates.AddRange(new NearMissSafeStartState[] {
    new NearMissSafeStartState { StatePKID = 1 },
    new NearMissSafeStartState { StatePKID = 2 },
    new NearMissSafeStartState { StatePKID = 3 },
    new NearMissSafeStartState { StatePKID = 4 }});

    dc.Incidents.InsertOnSubmit(oIncident);

    try
    {
    dc.SubmitChanges();
    }
    catch (Exception ex)
    {
    txtMessage.InnerText = ex.Message;
    }

    In terms of Best Practices how/when should children records be updated using
    the LINQDataSource and FormView controls?
    Jay Pondy, Apr 4, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Rodriguez
    Replies:
    23
    Views:
    1,110
    David Schwartz
    Dec 9, 2003
  2. shapper

    LinqDataSource

    shapper, Nov 9, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    322
    shapper
    Nov 9, 2007
  3. Rasmus
    Replies:
    0
    Views:
    459
    Rasmus
    Dec 12, 2007
  4. Jay Pondy
    Replies:
    9
    Views:
    846
    Jay Pondy
    Mar 28, 2008
  5. C.R.

    Formview and linqdatasource

    C.R., Mar 5, 2008, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    371
    Phil H
    Mar 8, 2008
Loading...

Share This Page