Implementing Inserts

You might remember from an earlier post that the application already has a button for adding a new presenter to the database.  Clicking the button shows the popup window; but instead of displaying blank fields ready for new data, it shows the details of whichever presenter’s details we had just viewed.  In addition, clicking the OK button of the popup window calls a function that tries to update the information about a presenter who already exists.  To solve these problems, a means is needed to keep track of whether existing data is being altered, or whether new information is being added.  To achieve this, I first of all added a global variable IsNew, and set it equal to true.  When details about an existing presenter are being displayed, I want to set IsNew equal to false.  I therefore added the line “IsNew = false;” to the onCommand function:

onCommandwithIsNew

I then made two additions to the command initialising the ModalPopup: a handler for the popup’s cancel button, and a handler for its showing event.

FinalModalPopup

The showing event is raised just before the popup is displayed on the screen.  The PopupShowing function, which handles the event, is as follows:

PopupShowing

If the IsNew global variable equals true, the function binds the PresenterDetails DataView to an empty object, which has the effect of setting all its fields to null.  When the New Presenter button is clicked, the resulting Popup looks like this:

PopupWithNulls

Lots of nulls!  To remove them, a convert function can be used:

ConvertFunction

This function takes one parameter, and returns an empty string if it equals null, and returns the parameter unchanged if it is not equal to null.  This function is used in the details view as follows:

DetailsWithNullConverters

The popup window now looks somewhat better:

PopupWithoutNulls

The same converter can be used in the master view:

PresentersWithoutNulls

When the OK button in the popup window is clicked, the SubmitChange function is called.  When that function was described in the last post, it had only to update an existing record.  Now, it must first check whether it needs to perform an update or an insert.

FinalSubmitChange

The first two lines retrieve the data from the details view, which is required in either case.  If IsNew equals true, an insert is performed with the insertEntity method.  This takes two parameters, the data to be inserted, and the name of the data type, which matches the name of the database table into which the data should be inserted.  The saveChanges method (which was described in the previous post) is then called.  Whichever operation is performed, the value of IsNew must be reset to true.

If the cancel button in the popup window is clicked, the CancelChange function is called.  No changes need to be made to the data, but IsNew again needs to be reset to true.

CancelChange

In my last post, I said I had three reasons for not binding the details view to the master view, and I gave two of them.  The third reason relates to inserts.  With the two views bound together, when the fields in the details view are cleared, I assume the associated data in the data context will also be cleared.  It seemed to me that this was likely to cause problems, and I could not work out how to temporarily disable data binding.

Advertisements

7 Responses to “Implementing Inserts”

  1. Shane Says:

    Do you have a download for this sample project?

    • politian Says:

      Thank you for your comments. Sorry, I don’t have a download available at the moment. This is my first blog, and I haven’t got everything organised yet.

      • Shane Says:

        Your off to a good start.

        Do you know how I could add a “blank” row to the data object returned from the web service? Basically, I have a table displaying my records and I want the users to be able to add a new row to the table and they could punch in the info there. I figured out how to use the “remove/removeat” functions but can’t get the “add/insert” working. Any suggestions?

        Thanks.

      • politian Says:

        If I understand you correctly, you are trying to do something similar to the example in my Handling Relationships Part II post. If that is so, you need to use the insertEntity method of the datacontext. If your table is bound to the data, I think you will then get a new row in which your user can insert data.

      • Shane Says:

        That’s what I needed. Thanks.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: