March 1, 2021
Hot Topics:

Getting Records from the Remote Device

  • By Nancy Nicolaisen
  • Send Email »
  • More Articles »

In this example, you are going to explore techniques for retrieving records from a remote database, once again looking into the functioning of the RemoteDBScan example program. If you are unfamiliar with the steps leading up to this point, you can review the previous lessons and the sample source code, which are all available on this Web site. Start from the point at which the user requests access to the database records.

Responding to User Requests for Record Retrieval

When the user selects a database from the list control and chooses the Remote Database Access.Get Database Records menu item, you land in the main frame window's menu handler.

This member function has three jobs:

  • It checks to make sure that user has selected a database from which to retrieve records.
  • It makes sure that the database actually contains records.
  • If both these conditions are met, it launches the dialog that displays the database records.

First, you check for a row selection by calling the CListCtrl member GetFirstSelectedItemPosition(). This function returns a value of type POSITION, but if there is no selected row, it returns NULL. This is subtle and tricky—the value returned, pos, is a 1-based index. The list control refers to items using 0-based indices. In the first step, you are only interested in whether or not there is a selection. If you want to manipulate the returned selection, you have to decrement and properly cast pos to land on the correct list view item.

void CMainFrame::OnGetDatabaseRecords()

    //get list ctrl
    CRemoteDBScanView* pListView =
    CListCtrl& ListCtrl = pListView->GetListCtrl();

    //get currently selected row
    POSITION pos = ListCtrl.GetFirstSelectedItemPosition();

    if( pos == NULL )
        AfxMessageBox( "You must select a database from the list.",
                       MB_OK, 0 );


Now, you use the returned position to query the item in which you stored the number of records contained in this database. You decrement pos, and assign this new value to a variable of type int, ipos. You set lvitem.iItem to ipos, and lvitem.iSubItem to 3, which is the 0-based index of the list control column containing the database record counts. You set the lvitem.mask to LVIF_TEXT, which signals that you are only interested in retrieving the item's caption string. You call ListCtrl.GetItem(), passing the initialized LVITEM structure as its parameter.

    //pos is a 1-based index, so you must decrement
    int ipos = (int)--pos;

    //get the item

    LVITEM lvitem;
    CHAR szBuff[12];

    memset( &szBuff, 0x0, sizeof(szBuff));
    lvitem.mask = LVIF_TEXT ;
    //pos is a 1-based index, so you must decrement
    lvitem.iItem      = ipos;
    lvitem.iSubItem   = 3;
    lvitem.pszText    = szBuff;
    lvitem.cchTextMax = sizeof(szBuff);


On successful return, you have the caption, and you compare it to 0—if there are no records in the database, you message the user and bail out here.

    int iLex = strcmp( szBuff, "0" );
    //message and bail if the item has no records
    if ( iLex == 0 )
        AfxMessageBox( "This database has no records.", MB_OK, 0);

If you clear all of these hurdles, it's time to open the remote database and recover the records. The CMainFrame class declares a member variable for the dialog that does this job. Here is an excerpt from the class header that depicts the relationship between the dialog and the CMainFrame class:

// Implementation
    CRecordListDialog m_RecordListDialog;

At the bottom of CMainFrame::OnGetDatabaseRecords(), you put up the modal dialog:

    //got a selection & some records, so put up the dialog


Page 1 of 2

This article was originally published on December 1, 2004

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date