3

I have an MFC application using OBDC to access the MS Access Database.

I am trying to insert a new record into the table, but I get a First-chance exception at 0x7705C44D in WinPASS.exe: Microsoft C++ exception: int at memory location 0x0039D470.

Here is the code

CDatabase db;
LPCTSTR lpszConnect = _T("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='';DBQ=C:\\Users\\New folder\\AccessDB.accdb;");
BOOL result = db.OpenEx(lpszConnect, CDatabase::noOdbcDialog);
CString test;
if (!result)
{
    AfxMessageBox("Could not find AccessDBfile");
}

CString sql;
sql.Format("INSERT INTO UserSelection( GroupName ) VALUES ( 'Something' )");

try
{
    db.ExecuteSQL(sql);
}
catch (CDBException* pe)
{
    pe->ReportError();
    pe->Delete();
    AfxMessageBox(pe->m_strError);
}

This code is actually on a loop, the first time running it will create the exception error on the second run AND insert the information in the table. So it basically works, this is an odd error. Does anyone have any idea of what might be wrong?

EDIT: Forgot to add, it doesn't crash just have that exception at ExecuteSQL. Also, CDBException does not catch that error.

Thanks.

Andrew Komiagin
  • 6,446
  • 1
  • 13
  • 23
Skynight
  • 507
  • 2
  • 7
  • 24
  • What does exception say? – Andrew Komiagin May 26 '15 at 04:46
  • There is no need to use `CString::Format()` method to generate SQL statement as you don't have any parameters. Simply pass your SQL stmt string as is to `ExecuteSQL()`. Call `AfxMessageBox(pe->m_strError);` before `pe->Delete();`. Otherwise you'll corrupt your memory. – Andrew Komiagin May 26 '15 at 04:57
  • Yep, nice catch on that one. – Skynight May 26 '15 at 05:57
  • The exception says First-chance exception at 0x7705C44D in WinPASS.exe: Microsoft C++ exception: int at memory location 0x0040CF30. If there is a handler for this exception, the program may be safely continued. – Skynight May 26 '15 at 05:57
  • Also, I need to use CString::Format() eventually, I'm just testing the simplest code I can have to find out what's causing this error first and then actually pass in parameters. – Skynight May 26 '15 at 05:58
  • Put `TRACE("%s %d", pe->m_strError, pe->m_nRetCode);` in your exception handler and run your app in debug mode (F5) to see the output. You can also put break point there to see what happens. – Andrew Komiagin May 26 '15 at 07:54
  • Hi Andrew, I put that in the catch but CDBException does not catch that. It breaks at AFX_ODBC_CALL(::SQLExecDirect(hstmt, reinterpret_cast(pszSQL), SQL_NTS)); which is at dbcore.cpp But the problem is that it requires acecore.pdb and I haven't been able to find that online. – Skynight May 26 '15 at 20:58

0 Answers0