Memory access violation using CRecordset and date/time fields 
Author Message
 Memory access violation using CRecordset and date/time fields

I have a CRecordset derived class that contains a CTime data member which
maps to a datetime field in the database.  If the table does not have any
records, then an access violation occurs in AFX.INL on line 283 during the
first Update.  If there is at least one record in the table, then everything
works fine.  The problem is getting that first record created.

Ex code:

someRecordset.AddNew();
someRecordset.m_SomeStringField = "foobar";
someRecordset.m_ADateField = CTime::GetCurrentTime();
someRecordset.Update();

During the RFX_Date method, there is a switch statement.  In the case
CFieldExchange::LoadField is where the problem seems to originate.  The call
to AfxCopyValueByRef causes the destination var (m_ADateField above) to be
set to a bogus value.  You know, 0xcdcdcdcd.

Has anyone else ran into this and do you have a workaround?  I was unable to
find anything about this problem in the knowledge base.  I am using VC 6
with service pack 3 against SQL Server 7.  I have also experienced the same
behavior using an Access 2000 database.

-Dave Muoio



Sun, 24 Nov 2002 03:00:00 GMT  
 Memory access violation using CRecordset and date/time fields

Quote:

> I have a CRecordset derived class that contains a CTime data member which
> maps to a datetime field in the database.  If the table does not have any
> records, then an access violation occurs in AFX.INL on line 283 during the
> first Update.  If there is at least one record in the table, then everything
> works fine.  The problem is getting that first record created.

> Ex code:

> someRecordset.AddNew();
> someRecordset.m_SomeStringField = "foobar";
> someRecordset.m_ADateField = CTime::GetCurrentTime();
> someRecordset.Update();

> During the RFX_Date method, there is a switch statement.  In the case
> CFieldExchange::LoadField is where the problem seems to originate.  The call
> to AfxCopyValueByRef causes the destination var (m_ADateField above) to be
> set to a bogus value.  You know, 0xcdcdcdcd.

> Has anyone else ran into this and do you have a workaround?  I was unable to
> find anything about this problem in the knowledge base.  I am using VC 6
> with service pack 3 against SQL Server 7.  I have also experienced the same
> behavior using an Access 2000 database.

> -Dave Muoio

I ran into this, oh, about 5 years ago and they still have not fixed it!  The
solution is to initialize your CTime member variable(s) in the recordset
constructor (the CRecordset-derived class that class wizard creates). It will
work if you set them to your birthday! (or any other valid date).

--
Scott McPhillips [VC++ MVP]



Mon, 25 Nov 2002 03:00:00 GMT  
 Memory access violation using CRecordset and date/time fields


Quote:
> I have a CRecordset derived class that contains a CTime data member
which
> maps to a datetime field in the database.  If the table does not have
any
> records, then an access violation occurs in AFX.INL on line 283
during the
> first Update.  If there is at least one record in the table, then
everything
> works fine.  The problem is getting that first record created.

> Ex code:

> someRecordset.AddNew();
> someRecordset.m_SomeStringField = "foobar";
> someRecordset.m_ADateField = CTime::GetCurrentTime();
> someRecordset.Update();

> During the RFX_Date method, there is a switch statement.  In the case
> CFieldExchange::LoadField is where the problem seems to originate.
The call
> to AfxCopyValueByRef causes the destination var (m_ADateField above)
to be
> set to a bogus value.  You know, 0xcdcdcdcd.

> Has anyone else ran into this and do you have a workaround?  I was
unable to
> find anything about this problem in the knowledge base.  I am using
VC 6
> with service pack 3 against SQL Server 7.  I have also experienced
the same
> behavior using an Access 2000 database.

> -Dave Muoio

Hi Dave

I had the same problem with MFC and Access 97.
What I did was as following CEmailInfoset is derived from Crecordset
IN the constructor I am intializing my CTime variable m_emailSentAT  to
currenttime. Apperently MFC does not initialize this CTime variable
anymore ( from 4.2 onward )- I think this is why it always work as long
as we had records in the database. I hope it will help - Best of luck
Belwo is sample from my program
Kd -

CEmailInfoset::CEmailInfoset(CDatabase* pdb)
        : CRecordset(pdb)
{
        //{{AFX_FIELD_INIT(CEmailInfoset)
        m_EmailInfoID = 0;
        m_emailSentTo = _T("");
        m_nFields = 3;
        //}}AFX_FIELD_INIT
        m_emailSentAT  = CTime::GetCurrentTime();
        m_nDefaultType = snapshot;

Quote:
}

Sent via Deja.com http://www.deja.com/
Before you buy.


Tue, 26 Nov 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Can't read time from Access 2000 date/time field

2. Using a date field in CRecordset

3. Date/Time field in Access db

4. Update Access 2000 Date/Time field error

5. Accessing Memo type fields using CRecordset

6. Change date field type from long to Date/Time

7. Using CRecordset::AddNew() and CRecordset::Edit() with IDENTITY fields

8. BUG: Memory Access Violation Uses Repeated Realloc's For Small Bl ocks Q225099

9. Problem with CRecordSet::SetFieldNull on Date field

10. Problem with CRecordSet::SetFieldNull on Date field

11. MFC bug when updating date fields in CRecordset ?

12. Updating date fields in CRecordset

 

 
Powered by phpBB® Forum Software