Article ID: 119765
Article Last Modified on 11/21/2006
void CSampView::DoDataExchange(CDataExchange* pDX)
{
CRecordView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSampView)
DDX_FieldText(pDX, IDC_EDIT_TITLE, m_pSet->m_strTitle, m_pSet);
DDX_FieldText(pDX, IDC_EDIT_AREA, m_pSet->m_strArea, m_pSet);
//}}AFX_DATA_MAP
DDX_FieldMemo( pDX, IDC_EDIT_MEMO, m_pSet->m_lbMemo, m_pSet );
}
/* Compile options needed: Default MFC application project options
*/
void DDX_FieldMemo( CDataExchange * pDX,
int nIDEdit, CLongBinary & lbMemo,
CRecordset * pRecordset )
{
ASSERT_VALID( pRecordset );
UINT nLen;
HGLOBAL hGlob;
LPSTR lpStr;
char * pStrWithNull;
HWND hWndCtrl = pDX->PrepareEditCtrl(nIDEdit);
if ( !hWndCtrl )
{
ASSERT(FALSE);
return;
}
if (pDX->m_bSaveAndValidate)
{
nLen = ::GetWindowTextLength(hWndCtrl);
if ( nLen )
{
hGlob = GlobalAlloc( GPTR | GMEM_SHARE, (DWORD)nLen );
if ( !hGlob )
{
AfxThrowMemoryException();
}
lpStr = (LPSTR)GlobalLock( hGlob );
if ( !lpStr )
{
GlobalFree( hGlob );
AfxThrowMemoryException();
}
// Allocate space for the '\0' string terminator
// Throws exception if needed
pStrWithNull = new char[nLen+1];
::GetWindowText(hWndCtrl, pStrWithNull, nLen+1);
// Cut off the null
#ifndef _WIN32
_fmemcpy( lpStr, pStrWithNull, nLen );
#else
memcpy( lpStr, pStrWithNull, nLen );
#endif
delete [] pStrWithNull;
GlobalUnlock( hGlob ); // Don't leave it locked.
}
else // Empty
{
nLen = 0;
hGlob = NULL;
}
// Free memory we are replacing
GlobalUnlock( lbMemo.m_hData );
GlobalFree( lbMemo.m_hData );
// Put in new data
lbMemo.m_dwDataLength = (DWORD) nLen;
lbMemo.m_hData = hGlob;
if (nLen == 0)
{
if ( pRecordset->IsFieldNullable(&lbMemo) )
pRecordset->SetFieldNull( &lbMemo, TRUE);
}
else
{
// It is required that we explicitly set it Dirty
// and NOT Null
pRecordset->SetFieldNull( &lbMemo, FALSE );
pRecordset->SetFieldDirty( &lbMemo, TRUE );
}
}
else // Reading data from recordset into control
{
nLen = (UINT)lbMemo.m_dwDataLength;
if ( nLen )
{
lpStr = (LPSTR)GlobalLock( lbMemo.m_hData );
// Throws exception if needed
pStrWithNull = new char[nLen+1];
#ifndef _WIN32
_fmemcpy( pStrWithNull, lpStr, nLen );
#else
memcpy( pStrWithNull, lpStr, nLen );
#endif
pStrWithNull[nLen] = 0; // Set '\0' at end of string
SetWindowText( hWndCtrl, pStrWithNull );
delete [] pStrWithNull;
GlobalUnlock( lbMemo.m_hData );
}
else
{
SetWindowText( hWndCtrl, "" );
}
}
}
Additional query words: SQL_LONGVARCHAR SQL_LONGVARBINARY MfcDatabase kbMFC kbVC150 kbVC151 kbVC152 kbVC200 kbVC210 kbVC400 kbVC410 kbVC420 kbVC500 kbVC600 kbVC600
Keywords: kbtshoot kbdatabase kbinfo KB119765