Changing the Application and Publishing Again

iIn this lesson, you will learn how to fix a bug in a LightSwitch application and then republish it.

Debugging an Application

The previous lesson, Running the Application, showed how to install and test the application. That lesson also identified a bug in the Find button on the ContactList screen. If there are no records in the Contacts entity, an error or exception occurs when the button is clicked. This lesson shows how to debug the application to find the error, and how to fix it by adding code in the Button_Execute() method to catch the exception and display a message that describes the problem.

To fix the error

  1. In Solution Explorer, select the ContactList node under Screens.

  2. On the Project menu, click View Screen Code.

    The Code Editor appears and displays the Find_Execute() method.

  3. Select the line of code that contains Application.ShowSearchCustomer(Contacts.SelectedItem.ContactName), and then on the Debug menu, click Toggle Breakpoint.

    Setting a breakpoint

    The line of code is highlighted and a red breakpoint glyph appears in the left margin.

  4. Press F5 to run the application, and then click the Contacts menu in the Navigation Menu to open the Contacts screen.

  5. Click the Find button on the toolbar.

    The Code Editor appears and the breakpoint is highlighted.

  6. On the Debug menu, click Step Into.

    Note

    A dialog box with a message regarding automatic step-over may appear. Click No to continue.

    A NullReferenceException was unhandled by user code message appears.

  7. On the Debug menu, click Stop Debugging.

  8. Replace the existing code in the Find_Execute() method with the following code.

    Try
        Application.ShowSearchCustomer(Contacts.SelectedItem.ContactName)
    Catch ex As Exception 
        ShowMessageBox("Please select a contact to find")
    End Try
    
    try
    {
        Application.ShowSearchCustomer(Contacts.SelectedItem.ContactName);
    }
    catch (Exception ex)
    {
        this.ShowMessageBox("No contacts exist to be found");
    }
    

    This code displays a message if an exception occurs.

  9. Select the line of code that contains Application.ShowSearchCustomer(Contacts.SelectedItem.ContactName), and then on the Debug menu, click Toggle Breakpoint.

  10. Press F5 to run the application again, and then click the Contacts menu in the Navigation Menu to open the Contacts screen.

  11. Click the Find button on the toolbar.

    The Code Editor appears and the breakpoint is highlighted.

  12. On the Debug menu, click Step Into.

    Notice that the exception message is not shown, and the highlight moves to the next line of code.

  13. On the Debug menu, click Step Into again.

    Notice that the highlight moves to the line of code in the Catch block.

  14. On the Debug menu, click Step Into a third time.

    The application window appears, and the Please select a contact to find message is displayed.

  15. Click OK to dismiss the message box.

    The Code Editor appears and the End Try (closing brace in C#) code is highlighted.

  16. Press F5 to resume running the application, and then click the Close button.

To republish and test the application

  1. In Solution Explorer, double-click the Properties node.

  2. On the General tab of the Application Designer, choose the Application version field and in the second text box, type 1001.

  3. On the Build menu, click Configuration Manager.

    The Configuration Manager dialog box appears.

  4. In the Active Solution Configuration list, choose Release, and then click Close.

  5. On the Build menu, choose Publish My First Application.

    The LightSwitch Publish Application Wizard appears.

  6. In the lefthand pane, choose Database Configuration.

    The Database Configuration page appears.

  7. Clear the Generate the SQL database script check box.

  8. In the lefthand pane, choose Authentication.

    The Application Administrator page appears.

  9. On the Specify Authentication configuration page, in the Should Authentication Information be deployed with the application? section, select No, Authentication information has already been deployed.

    Note

    Deploy authentication information only the first time you publish an application. If you attempt to deploy the same authentication information again, publishing will fail and the The username is already in use message will be displayed.

  10. Click Publish to republish the application.

    Wait until the Publish succeeded message appears in the status bar.

  11. On the Start menu, click Northwind Application to open the application.

    A message is displayed and the application is automatically updated to the new version. When installation is complete, the Northwind Application will appear.

  12. Click the Contacts menu in the Navigation Menu to open the Contacts screen.

  13. Click the Find button on the toolbar and verify that the Please select a contact to find message that you added earlier is displayed.

  14. Click the Close button to close the application.

Closer Look

This lesson showed how to debug, update, and republish an application. The first step was to set a breakpoint in the Find_Execute() method. When the application ran, it stopped just before the line of code at the breakpoint so that you could step into the code and execute just that line.

As that point, the code stopped executing and displayed a Null Reference Exception message. A Null Reference Exception occurs when code references an object that does not exist; in this case, the object is the ContactName for the SelectedItem.

This lesson then showed how to fix the code by adding a Try…Catch block. In a Try…Catch block, the code in the Try block is executed first. If an exception occurs, the code in the Catch block executes, in this case, it displays a message box to the user.

After the lesson showed how to fix the bug, it showed how to again step through the code to verify that the exception was handled and the code in the Catch block was executed and displayed the message box. It is a good idea to add a Try…Catch block to your code anywhere that an exception may occur.

Finally, the lesson showed how to update the Application Version number and then republish the application. Notice that when you installed the application, you were not prompted for permission to install. Permission was granted the first time that you installed the application and is not required for updates.

Next Steps

In the next lesson you will how to use the administration features in the application.

Next lesson: Acting as the Application Administrator

See Also

Concepts

How to: Deploy a 2-tier Application

Other Resources

Distributing Your Application

Debugging: Finding and Fixing Errors

Deployment: Distributing and Maintaining Your Application