LightSwitch Data – If you have followed the previous two articles I wrote (Visual Studio LightSwitch – True Power Lies In Simplicity and Extending LightSwitch – Deeper Down The Rabbit Hole), you will by now know that our little application is becoming more sophisticated and feature rich.
I have to hasten to add, our application is becoming more feature rich without us having to do much work. It seems almost like stealing candy from a baby, and I almost feel guilty for getting so much satisfaction from creating with LightSwitch.
If by now you aren’t hooked on LightSwitch, I don’t know what will get you hooked. The more I play with LightSwitch, the more I love it. It is easy to see why there is such a large community behind this technology.
As I have mentioned before, it is the data that drives LightSwitch. The underlying data layer needs to be correct, so a basic understanding of databases is preferable. But the technology is also very forgiving. Give it a few hours time, and you will be quite fluent with it.
LightSwitch Data – Make Changes Effortlessly
Let us assume that you have started creating your application but you now need to add some additional fields to one of your existing tables. This could be as a result of a user request or that you simply forgot to add it in the first place. Changing the data will undoubtedly change the form. You might be apprehensive because you don’t want to break something by modifying the underlying tables. Well let me assure you, LightSwitch is very robust. Modifying tables and changing your screens is just as easy as the previous tasks we performed.
Start by going to your Solution Explorer pane in Visual Studio again.
Select the Members table.
For arguments sake, let us just add some additional detail here with regards to the next of kin.
Next, you need to select the Members screen to add the additional fields.
This time we are going to add these fields to our screen a little differently.
In the left field list, select the ‘Father’ field and drag it under your ‘Group’ field. You will see a blue line marking the position where your additional field will be added.
After you have added the ‘Father’ field, you can go ahead and add the other additional fields previously added to your table.
Save the LightSwitch solution in Visual Studio and run your app. After a few seconds, your form will be displayed and you will see the additional fields added to your application. This was easy enough. In no time, we were able to modify the underlying table fields and directly influence the screen design by associating the data to the screen via text controls. But let us imagine that another additional field is required that needs to store additional data. This time a single line text entry will not work.
Go ahead and modify your Members table and add a field called ‘Additional Info’. Associate this field with your members screen as we did in the previous steps above.
Take note of the Properties section to the right of the Customization Mode screen for Members when you select the Additional Info field.
Have a look under the Sizing group, towards the bottom (you might need to scroll down) there will be a ‘Lines’ radio button. Select this and change the default 1 line to 10 lines.
Save these changes in the designer and return to your Windows application. You will now see that the Additional Info field is now in actual fact a text area. These changes were once again done with absolute minimal effort and allowed us to change the underlying data that drives the form. Let us now go and paste a bunch of text into our Additional Info field.
Whoa, stop the presses. Adding a whole lot of text to our Additional Info field caused a validation error. Luckily LightSwitch allows us to ‘Click here for more details’ to get more information on the error being reported.
Doing this allows us to see that the Additional Info field only allows us to add a maximum of 255 characters. Well this is a fine how do you do. Once again, LightSwitch makes this quite easy to change. Stop your application from running and return to the Members table.
In the properties of the Members table for the Additional Info field (near the bottom) you will see that the field length is defined as 255.
Change this value to a larger value. I made mine a healthy 1500 characters.
If you run your application again, you will notice that you can now easily add and save the large amount of text you added previously into the Additional Info field.
LightSwitch has once again come through for us. We are programmers! Defenders of the Lazy and don’t-reinvent-the-wheel type personalities. With minimal effort we have once again made significant changes to our application. I know that I am steam rolling through many of these features, but there are just so many that lie ahead, just waiting to be discovered. Go ahead and open a whole new world for yourself. Explore LightSwitch. You can thank me later.