Most of the applications I do in LightSwitch involve some kind of single top-level entity, such as a company. Here is a tip on how to make sure that entity exists when the application first fires up…
When it comes to the outputs of an application, there is almost always going to be some high-level reference information that will be required. A good example of this is in the header and footers of reports where the name, and sometimes address, of the organization needs to be output.
Such is the case with the applications I have been building. An invoice, for example, will require the company name and address information on it. Or maybe there is some high-level defaults, such as a tax rate, that need to be used elsewhere in the system.
Building the tables and screens to manage this information is the easy part. But what if I had a constraint that information must exist when I first launch the application. Here is a simple, but effective, solution to solve this; using the Initialize event of the application.
In the scenario for this example, my “Company” information needs to exist when I first launch the application. This is so I can use the information for setting values on some screens, such as for displaying the name of my company on the top of each screen.
Yes, I already have a Company table as well as the appropriate screen to edit the company information. However, I want to save myself some time and not have to worry about evaluating whether or not the Company entity is null each time I needed to reference it.
So, here is what I did…
I opened up the properties dialog for my project, and selected to view the application code via the Screen Navigation tab…
Next, I simply select to add some custom code the application initialize event…
The custom code I add simply checks for an existing entity in my Company table, and creates one of there is not one there…
…here’s the code I used for setting the default…
private void SetDefaultCompany()
{
DataWorkspace dataWorkspace = Application.Current.CreateDataWorkspace();
Company company = dataWorkspace.ApplicationData.Companies.FirstOrDefault();
// if there is no company record yet, create one.
if (company == null)
{
company = dataWorkspace.ApplicationData.Companies.AddNew();
company.CompanyName = "<<Enter your company name...>>";
dataWorkspace.ApplicationData.SaveChanges();
}
}
So, with that, I don’t have to do much work later to evaluate whether a company record exists or not, because I know it will always be there because of this simple, but effective, process.
For example, here is the screen where the Company entity can be edited. I don’t have to worry about the “Add” functionality, nor do I need to use up screen real-estate with a useless grid; because I am only one company…
I’ve also used this process in the initial deployment of an application. For example, to populate tables that users will not have access to, such as reference or look-up type tables.
Hope you find it useful.
Cheers!

























No comments yet. Be the first!