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()
{
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;
sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s",sDriver,sFile);
TRY
{
database.Open(NULL,false,false,sDsn);
CRecordset recset( &database );
SqlString = "SELECT CatID, Category "
"FROM Categories";
recset.Open(CRecordset::forwardOnly,SqlString,CRecordset::readOnly);
ResetListControl();
ListView_SetExtendedListViewStyle(m_ListControl,LVS_EX_GRIDLINES);
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( );
CDBVariant varValue;
while( !recset.IsEOF() )
{
for( short index = 0; index < nFields; index++ )
{
recset.GetFieldValue( index, varValue );
switch ( index ) {
case 0:
if ( varValue.m_dwType == 8 )
iRec = m_ListControl.InsertItem(0,varValue.m_pstring->GetBuffer(),0);
break;
case 1:
if ( varValue.m_dwType == 8 )
m_ListControl.SetItemText(0,1,varValue.m_pstring->GetBuffer());
break;
}
}
recset.MoveNext();
}
recset.Close();
database.Close();
}
CATCH(CDBException, e)
{
AfxMessageBox("Database error: "+e->m_strError);
}
END_CATCH;
}
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);
}
}