Click here to Skip to main content
16,015,583 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am new in VC++ programming and using Microsoft Visual C++ .NET. I have run the program in the article: Using the CDatabase class to read an Access databases[^].

But it generates errors on building. I modified the function below as follows. The ResetListControl()function remains as it is. It runs without errors but does not show the data in the database.

Please help..

void CReadDBDlg::OnRead() 
{
    // TODO: Add your control notification handler code here
    CDatabase database;
    CString SqlString;
    CString sCatID, sCategory;
    CString sDriver = "MICROSOFT ACCESS DRIVER (*.mdb)";
    CString sDsn;
    CString sFile = "C:\\Documents and Settings\\Owner\\Desktop\\" 
                    "mfcdataAccess\\readDb\\ReadDB_demo\\Test.mdb";
    int iRec = 0; 
    // Build ODBC connection string
    sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s",sDriver,sFile);
    TRY
    {
        // Open the database
        database.Open(NULL,false,false,sDsn);
        // Allocate the recordset
        CRecordset recset( &database );
        // Build the SQL statement
        SqlString = "SELECT CatID, Category "
                    "FROM Categories";
        // Execute the query
        recset.Open(CRecordset::forwardOnly,SqlString,CRecordset::readOnly);
        // Reset List control if there is any data
        ResetListControl();
        // populate Grids
        ListView_SetExtendedListViewStyle(m_ListControl,LVS_EX_GRIDLINES);
        // Column width and heading
        m_ListControl.InsertColumn(0,"Category Id",LVCFMT_LEFT,-1,0);
        m_ListControl.InsertColumn(1,"Category",LVCFMT_LEFT,-1,1);
        m_ListControl.SetColumnWidth(0, 120);
        m_ListControl.SetColumnWidth(1, 200);
        short nFields = recset.GetODBCFieldCount( ); //get total no. of fields
        CDBVariant varValue;
        // Loop through each record
        while( !recset.IsEOF() )
        {
            for( short index = 0; index < nFields; index++ )
            {
                // Copy each column into a variable
                recset.GetFieldValue( index, varValue );
                switch ( index ) {
                    case 0: // field no 1
                        if ( varValue.m_dwType == 8 )
                            //Insert values into the list control
                            iRec = m_ListControl.InsertItem(0,varValue.m_pstring->GetBuffer(),0);
                        break;
                    case 1: // field no 2
                        if ( varValue.m_dwType == 8 )
                            m_ListControl.SetItemText(0,1,varValue.m_pstring->GetBuffer());
                        break;
                }
            }
            // goto next record
            recset.MoveNext();
        }
        // Close the database
        recset.Close();
        database.Close();
    }
    CATCH(CDBException, e)
    {
        // If a database exception occured, show error msg
        AfxMessageBox("Database error: "+e->m_strError);
    }
    END_CATCH;
}

// Reset List control
void CReadDBDlg::ResetListControl()
{
    m_ListControl.DeleteAllItems();
    int iNbrOfColumns;
    CHeaderCtrl* pHeader = (CHeaderCtrl*)m_ListControl.GetDlgItem(0);
    if (pHeader)
    {
        iNbrOfColumns = pHeader->GetItemCount();
    }
    for (int i = iNbrOfColumns; i >= 0; i--)
    {
        m_ListControl.DeleteColumn(i);
    }
}
Posted
Updated 19-Mar-10 10:00am
v4

1 solution

First of all, you are responding to an article...as you clearly said, so you should post your question on that page. At the bottom of the article is a forum for questions such as this.

Secondly, when you post that much code, you need to tell us what errors are being generated and on which lines. Otherwise, we're not going to go through each line and try to figure out the problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900