Introduction to PowerBuilder

HomePrevious Lesson: Adding a Row to the DataWindow
Next Lesson: Saving Changes to the Database

Deleting a Row from the DataWindow

// Object: cb_delete in window w_product_master
// Event: clicked
long lDeleteThisRow
int lUserAnswer, lItemNo, lUpdateStatus
lDeleteThisRow = dw_product.GetRow()
If lDeleteThisRow > 0 then
   lItemNo = dw_product.GetItemNumber( lDeleteThisRow, 1)
   lUserAnswer = MessageBox("Delete","Do you " + &
                     " want to delete product #" +  & 
                     String(lItemNo),&
                     StopSign!, YesNo!, 2)
   if lUserAnswer = 1 then
      dw_product.DeleteRow( lDeleteThisRow)
      lUpdateStatus = dw_product.update()
      if lUpdateStatus = 1 then
         Commit using SQLCA;
      else
         RollBack using SQLCA;
         MessageBox("Delete", "Error in deleting " & 
                  + String(lItemNo) + "Error Message: " &
                     + SQLCA.SqlErrText)
         return -1
      end if
   end if
end if

GetRow() and DeleteRow() functions are usually paired. We use the GetRow() function to return the current row. If it exists, it returns an integer greater than zero. Depending on this value, we ask the user to confirm the deletion. If they confirm it, we call DeleteRow() and Update() functions and commit the deletion. If there are any problems, we roll it back and display an appropriate error message.

If only the DeleteRow()is called, PowerBuilder deletes the row from the DataWindow only and the changes are not reflected onto the database. When using the Delete CommandButton, some applications implement deleting the rows from the DataWindow. They save the deleting from the database part for the Save CommandButton. Both are fine. How it is implemented depends on the user requirement. Some users would want the functionality of un-deleting rows for later.

Note that Commit and Rollback are database related commands, and so should be terminated with a semicolon.

For each function you learn here, we advise you to read PowerBuilder online help in detail.

GetItemNumber() returns the value of the specified row and column. This function is used when the column datatype is numeric. If the column datatype is string, you need to call GetItemString(). We can as well ask the user 'Do you want to delete record #??'. However, our motto is to make it as user friendly as possible, hence, it would be useful if prompted with the product number or the product description. That's why, we are calling GetItemNumber()to get the product number. Other useful functions to study are GetItemDateTime().

You may not be familiar with the words commit and rollback. This is transaction processing in action. While using the SetTransObject(), we have to take care of committing or rolling back changes to the database. If a commit statement is used, it means that the changes are confirmed and they are to be reflected in the database, but then if we rollback the changes, the database has to return to the state it was before the Update() function was called.
HomePrevious Lesson: Adding a Row to the DataWindow
Next Lesson: Saving Changes to the Database