Solved

MFC, CRecordset::PrepareAndExecute() Error using MSSQL-Sever-2005

Posted on 2010-08-15
7
653 Views
Last Modified: 2013-11-20

Hello,

I connect to an MSSQL DB 2005
Connection to DSN used in CDatabase Tested Ok.

Before I insert rec to the DB table I open the a CRecordset with

CRecordset::Open(nOpenType, ssQueryStr, dwOptions);

nOpenType = CRecordset::dynaset
ssQueryStr = select * from mytable where 1 = 0   // returns no records
dwOptions = 0
(with plan to call AddNew() and Update() later)

But this throws exception in CRecordset::PrepareAndExecute() at

// now attempt to execute the SQL Query if not executed already
if (!(m_dwOptions & executeDirect))
{
    AFX_ODBC_CALL(::SQLExecute(m_hstmt));
    if (!Check(nRetCode))
      ThrowDBException(nRetCode);
}


This had worked before.

Please any suggestion to the cause, solution or workarround to this problem?

TBlog
 
0
Comment
Question by:tblog
  • 4
  • 3
7 Comments
 

Author Comment

by:tblog
ID: 33442638

I dev with Visual C++/MFC env.

TBlog
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 33443360
Are you passing values that would allow a record to be added?

eg.  Passing null to a required field would result in a failure
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 33451996
Have you checked the values you pass yet ?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:tblog
ID: 33452476

I am not passing any null.

nOpenType = CRecordset::dynaset = 0
ssQueryStr = select * from mytable where 1 = 0   // returns no records
dwOptions = 0

are passed to

CRecordset::Open(nOpenType, ssQueryStr, dwOptions);



0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 33452507
I don't think you understood.
For example:
Say the table has three fields (columns) that are required.  Just setting two fields to values then attempting to update will result in an error.

Check the values you are attempting to add to the table - are they valid for the field types expected ?  Are any required fields not being given values ?
0
 

Author Comment

by:tblog
ID: 33869384
Thanks guys. This was fixed by the database init configurations params. One thing I did to fix this was to specify default DB on the host. Strange.
0
 

Author Closing Comment

by:tblog
ID: 33869391
Tried solution combined with other things
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now