Overview of Article
LightSwitch automatically creates dropdown boxes if relationships are correctly set up between tables. It steers you towards creating correct data relationships which is a good thing.
However, there are exceptional circumstances when relationships have not or can not be set up. Connecting to legacy data is a good example of such a scenario. One option is to set up a ‘Choice List’ at the entity level but this isn’t necessarily appropriate if there are many items or if the items already exist in a database table.
Fig1: Select the column, view the properties and click on the ‘Choice List’ hyperlink.
The following article addresses how to fill an AutoCompleteBox with database values when relationships have not been set up at the table level. We’ll be creating an ‘Add Data’ form as part of this example. The following example uses a Customer table containing a string column called Category. The screen contains an AutoCompleteBox with a constrained list of category values. The selected string value will be saved into the Customer>Category field.
Data
The following screenshots show the tables that have been set up. Notice that no relationships have been set up between these 2 tables.
Screens
We now come on to create our screen…
- Create a ‘New Data Screen’ based upon the customer table
- Click on the ‘Add Data Item’ button and create a ‘Local Property’ of type ‘Category (Entity)’. Name it prpCategory.

Fig4 Creating prpCategory
- Delete the Category textbox which is created by default.
- Drag prpCategory from the left hand navigation pane onto your screen. Your screen should like this:

Fig5 This is how the screen should appear - Click on the ‘Write Code’ button and select the ‘CreateNewCustomer_Saving’ option.
- Write the following code:
Private Sub CreateNewCustomer_Saving(ByRef handled As Boolean) Me.CustomerProperty.Category = prpCategory.CategoryName End Sub
The C# equivalent would look like this:
partial void CreateNewCustomer_Saving(ref bool handled) { CustomerProperty.Category = prpCategory.CategoryName; }
Conclusion
Now run the solution and you’ll be able to use the Category drop down to fill the Customer > Category field. The source for this example can be downloaded through the following link.
http://cid-7a042700ee409d7b.office.live.com/self.aspx/LightswitchFiles/CustomerCategory.zip



Another excellent article Tim!
Great example Tim — very helpful.
I’m using the same screen in Lightswitch Beta2 for adding and editing records (as per Beth Massi’s example here: http://msdn.microsoft.com/en-us/lightswitch/Video/ff945359). I’ve managed to create an autofill control that looks up entries from my lookup table. The code you’ve posted above works great for updating the field value based on the control.
Problem — when the screen is in *edit* mode, the autofill box is blank. I tried to update the control value based on the field value, basically the equivalent of:
prpCategory.CategoryName = Me.CustomerProperty.Category
It didn’t work — I got a NullReference error. Any idea what I might be doing wrong?
Thanks in advance.
Hi JD,
Glad you found the example helpful!
When returning to the screen in edit mode, you’ll need to set prpCategory to your desired Category value rather than setting the CategoryName attribute of the category. There’s a bit more info in the following thread but the syntax would look something like this:
prpCategory = DataWorkspace.ApplicationData.Catogories.Where(Function(cat) cat.CategoryName = Me.CustomerProperty.Category).FirstOrDefault()
http://dotnettim.wordpress.com/2011/03/16/lightswitch-setting-combobox-values-in-code/
Hope that helps,
Tim
Thanks for your response Tim. I’m having trouble setting prpCategory (prpCity in my case) to anything. My data source is MSSQL, so instead of adding prpCity as an entity, I added it as a table (my lookup table). If I try to set prpCity to a string value, I get the message “Value of type ‘String’ cannot be converted to ‘LightSwitchApplication.lookup’ ” Same thing if I try to set it to an int value.
Any ideas?
Never mind — your method worked perfectly once I got the syntax right. Thanks so much for your help!
Hi JD,
Glad you got this to work!
For the benefit of other readers, prpCity is of type LightSwitchApplication.lookup. For that reason, you would get that type of error if you wrote something like prpCity = “London”.
By setting prpCity to a ‘lookup’ object, you get around this problem.
prpCity = DataWorkspace.ApplicationData.Lookups.Where(Function(l) l.CityName = Me.CustomerProperty.CityName).FirstOrDefault()
Tim
Hi Tim,
The code worked flawlessly for a AddNewCustomer screen. But I am facing issues on the List and Edit screen. I am unable to find CustomerProperty. I am trying to set the value of prpCategory in the ManageCustomer_Created() function of the screen. Can you pls help here.
I noticed that Tim’s method works great if the CustomerProperty field value is a string. However it didn’t work when the value is a byte (for example in my incomeLevel field I store a byte that corresponds to a value in the lookup table). I think it didn’t work because the summary field in my lookup table is the string description, so the function couldn’t find a match searching for lookup.incomeLevel = CustomerProperty.incomeLevel.
In any case I found an alternate solution, as follows:
1. Add a new lookup query to the screen that returns the values for income level (in my case lookupCategory = “incomeLevel”), name it qIncomeLevelValue
2. Add a parameter to the query called theIncomeLevel
3. Bind theIncomeLevel to CustomerProperty.incomeLevel
4. The query will return one row — with the value that should populate the incomeLevel autofill box.
5. In screenName_activated, add this code:
Me.prpIncomeLevel = Me.qIncomeLevelValue.FirstOrDefault()
So here’s my next question. When the screen is in edit mode, and I change the value of one of the autoComplete controls and hit Save, the value isn’t saved (because I’ve changed a control value, but not a field value). Is there a way to trigger the code that updates the CustomerProperty field from the control when the control value is changed (and no other fields are changed)?
Thanks again!
Hi JD,
Thanks for the info about the byte fields
In answer to the question about running some code when the control value changes, you can handle the ‘LostFocus’ event of the AutoCompleteBox and write the code to update the CustomerProperty field from there.
Here’s an article on using the LostFocus event to add non existent records using the AutoCompleteBox. Hopefully, you’ll be able to use the LostFocus technique that’s demonstrated there to set your field when the control value changes.
http://dotnettim.wordpress.com/2011/04/20/lightswitch-add-non-existent-records-using-autocompletebox/
The article contains a method called CategoryFieldChanged. This is the bit of code that you would adapt to suit your scenario.
Hope that’s of some use to you!
Tim
Thanks Tim — that worked perfectly. And congratulations on your award!
Pingback: LightSwitch – Fill AutoCompleteBox with Database values | Kingsley A. Bainn
I am trying to find a similiar solution to pass a database value to a form for adding a new many to many relationship.
The case might be Many Customers with Many Groups joined by a table of CustomerGroups.
IE A user selects a Group from an existing list of groups and wants to add a New Customer to that Group (as oppose to adding a customer that already exists in the database).
See forum question here for further explanation:
http://social.msdn.microsoft.com/Forums/en-US/lightswitchgeneral/thread/21e262a5-1f5e-48fe-af00-e0b01d9af624
I haven’t seen any exmaples/tutorials on how to achieve this, and any help would be great.
Hi Bowlsy, thanks for the question. I take it that you found the answer via JamieH’s thread here?
http://social.msdn.microsoft.com/Forums/en-US/lightswitchgeneral/thread/60842e95-ba56-4162-9db2-2aac975dba8a
Tim
Yes Thanks. He mentioned working on making the form open as model window. That would also be handy in some cases.
Great article! I noticed that some of the comments also touched on the same difficulties I was experiencing while attempting to use a single screen for editing and deleting. I was wondering if there is a way to update the screen name (ie. Tab) with the name of a new entity just saved in situations where a single screen is used for editing and adding? It would be great if someone on the lightswitch could create a video covering the culmination of these concepts…
Hi Benjamin, thanks for your comment.
The DisplayName property should allow you to set text displayed in a tab. Maybe you could use this to code something up?
this.DisplayName = “Set the Screen Name Here”; //C#
Me.DisplayName = “Set the Screen Name Here” ‘ VB
Tim
Hi Tim,
If you don’t mind, I have another question related to the pre-populating of property entities (used for lookups into another table) when going back into a screen in edit mode (as with Beths; example)… I’m a bit new to C# which is what I need to use so my question may be (in part) due to ignorance of the synax…
In your response to J.D. on may 3rd, you reference the code below as an example to pre-populate the lookup value for the entity property on the screen:
prpCategory = DataWorkspace.ApplicationData.Catogories.Where(Function(cat) cat.CategoryName = Me.CustomerProperty.Category).FirstOrDefault()
I was wondering if you can provide a C# example and also confirm where this would be coded (going back into edit mode)… I would think this would be in the Initialize Data Workspace method as per my example below???
public partial class CreateNewRequestEdit
{
partial void CreateNewRequestEdit_InitializeDataWorkspace(List saveChangesTo)
{
// Write your code here.
if (this.RequestID != null)
this.RequestProperty = this.Request;
// Prepopulate the entity property here???
else
this.RequestProperty = new Request();
}
Here’s what I have in my Saving method which seems to save properly.
partial void CreateNewRequestEdit_Saving(ref bool handled)
{
// Write your code here.
RequestProperty.Responsible = prpResponsible.Employee;
RequestProperty.Manager = prpManager.Employee;
RequestProperty.TechResponsible = prpTechResponsible.Employee;
RequestProperty.TechManager = prpTechManager.Employee;
}
In my case, I have an entity named “Requests” which has a few colums which reference responsible employees (for certain tasks) and all the responsible employees are maintained in a single table (Responsible).
Thanks for any insite you can provide…
Hi Benjamin,
The C# version will look like this:
prpCategory == DataWorkspace.ApplicationData.Catogories.Where(cat => cat.CategoryName == this.CustomerProperty.Category).FirstOrDefault();
The InitializeDataWorkspace should work but if not, the Activated method should work too.
Tim
Thanks Tim…
Hello Tim. I am a newbie of newbies in Lightswitch. I have the problem I have been cracking my head on. I am working on an app with entities where a patient in a PatientRecord table has multiple immunization entries in Immunization table. I have created a master detail with Patient record and Immunization the problem is when I want to create a new immunization entry for a patient I will like to have some of the fields in the new data screen filled up by data from the patient record. Please help
Hi Aklama,
On your immunization screen, the patient record appears by default as an autocompletebox. At the bottom of your screen, add patient again so that you have 2 autocompleteboxes bound to patient. In the 2nd instance that you’ve added, use the dropdown box to change patient to a ‘group control’ (ie a columns layout). The remaining patient details will now be shown as textboxes. You can change these to labels to make them read only.
Hope that helps!
Tim
Thank you so much for your quick reply Tim. I really do appreciate it. I will give a go and revert back ASAP
Hi Tim,
Thanks again for helping out.
I implemented your suggestion but I am still not getting it to work. Perharps my question was not clear.
The main Screen which open first is a PatientListDetail Screen which has immunization details also so that as you go from patient to patient their immunization records are displaced at the bottom right hand of the screen. My intention is to replace the default add immunization screen template with the one I created. The problem is when I click the ADDNEWEDIT button on the immunization screen, it does not preselect the patient for which I intend to create the record for. I hope it is clearer now.
Thanks in advance
Aklama