Add On-Hire Assets
Now
that we have documents in our document library, we can move on and
create list items that refer to those documents. Double-click the Add
On-Hire Assets button on the form designer to add an on-click event
handler. Add the following code:
private void button2_Click(object sender, EventArgs e)
{
//Add some assets for our contracts
//disable the button to prevent concurrency problems
button2.Enabled = false;
using(HireSampleDataContext dxWrite = new HireSampleDataContext(SiteUrl.Text))
{
//since we'll be making changes, we must set ObjectTrackingEnabled to true
//This is the default value but is explicitly included here
//for the sake of clarity
dxWrite.ObjectTrackingEnabled=true;
int counter = 0;
//loop through the contracts in our Hire Contracts document library
foreach (HireContract contract in dxWrite.HireContracts)
{
//Add new assets for each contract
for (int x = 0; x < 3; x++)
{
OnHireAsset newAsset = new OnHireAsset();
//generate sequential sample data
newAsset.AssetId = counter.ToString("ASSET000");
newAsset.AssetTag = counter.ToString("TAG-000");
newAsset.ContractReference = contract;
//set the new asset entity to be added into the OnHireAssets list
dxWrite.OnHireAssets.InsertOnSubmit(newAsset);
counter++;
}
}
//Submit the changes. This will actually add the items to the list
dxWrite.SubmitChanges();
}
//change the text on the button so that we know the job's done
button2.Text += " - Done";
}
Notice first, and most importantly, that everything
is strongly typed, meaning that if the code contained any errors, it
would not compile. Second, the code is much more descriptive than the
preceding example. It’s apparent which data entities we’re dealing with
and what actions we’re performing on them purely from the syntax of the
code. Finally, each On-Hire Asset must have an associated Hire Contract. In this code,
the assignment of a parent Hire Contract is done by simply assigning an
appropriate HireContract entity to the ContractReference
property that defines the relationship. There’s no need to know exactly
which field is used to define the foreign key since all of that
information is automatically determined by the configuration of the
lookup field.
Add AssetNotes
We’ve
created sample contracts and associated assets; the next thing to add
is Asset Notes for each asset. Double-click the Add Asset Notes button
to add an on-click event handler. Add the following code:
private void button3_Click(object sender, EventArgs e)
{
//disable the button to prevent concurrency problems
button3.Enabled = false;
//Add some notes for our sample assets
using(HireSampleDataContext dxWrite = new HireSampleDataContext(SiteUrl.Text))
{
dxWrite.ObjectTrackingEnabled = true;
foreach (OnHireAsset asset in dxWrite.OnHireAssets)
{
for (int x = 0; x < 3; x++)
{
AssetNote newNote = new AssetNote();
newNote.LocationCode = x.ToString("Location000");
newNote.AssetReference = asset;
dxWrite.AssetNotes.InsertOnSubmit(newNote);
}
}
dxWrite.SubmitChanges();
}
//change the text on the button so that we know the job's done
button3.Text += " - Done";
}
You should now have a Windows Forms
application with three buttons that can be used to generate some sample
data. Run the application, clicking each of the three buttons in turn
to add some sample data.