Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>So effectively you want to wrap the two LinqDataSources in a single transaction. There is a sneaky way to leverage the LinqDataSource databinding and events and still do only one commit. Using this method you can still use Dynamic Data, FormView, GridView, validation, etc. It only relies on hooking into the data sources.</p> <p>Example markup:</p> <pre><code>&lt;FormView&gt;...&lt;/FormView&gt; &lt;ListView&gt;...&lt;/ListView&gt; &lt;LinqDataSource ID="dsEvent" ...&gt; &lt;/LinqDataSource&gt; &lt;LinqDataSource ID="dsContact" ...&gt; &lt;/LinqDataSource&gt; &lt;asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit it All!"/&gt; </code></pre> <p>Now in the code behind you are using the single Submit button to mimic the behaviour of the LinqDataSources. You use the datasource to insert the item, which creates the new object, snaffle the object, do it again for the second object. Link the two items together and any other logic you want and then insert it into the database as a single transaction. The key is to set the Cancel flag in the Inserting event so that the datasource doesn't actually insert into the database.</p> <pre><code>protected void btnSubmit_Click(object sender, EventArgs e) { Event evt = null; Contact contact = null; dsEvent.Inserting += (o,ea) =&gt; { evt = (ea.NewObject as Event); ea.Cancel = true; }; dsEvent.InsertItem(true); dsContact.Inserting += (o, ea) =&gt; { contact = (ea.NewObject as Contact); ea.Cancel = true; }; dsContact.InsertItem(true); evt.Contacts.Add(contact); using (var dbContext = new ContactsCalendarEventsXtabs()) { dbContext.Events.InsertOnSubmit(evt); dbContext.SubmitChanges(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload