Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have consumer(class) generated by ATL OLE DB Consumer.

CProduct m_products; <---- that's a object(Represents table in DB);

Like this I try to show all data in DB

C++
CoInitialize(NULL);
hr = m_products.OpenAll();//That's S_OK
    m_products.MoveFirst();
    do{
        if(m_products.m_ProductId == id)
        { 
            m_products.Name = "fhsfj";
           //Doing something with binded variables to columns in table
        }
    }while(m_products.MoveNext() == S_OK);
    m_products.CloseAll()


But first I want Insert new data in my empty table like this:

C++
m_products.m_Price = //add something;
m_products.m_Quantity = //add something;
//all variables beside Id, I initialized in code.Id in db(auto increment)
m_products.Insert();//DB ERROROCCURED
m_products.SetData();

It's doesn't work I can't insert nothing in DB.Please help me
How Create Get, data from DB?
Posted

you need to initialise the variable for m_ProductId with the status, like this:
C++
class CProduct:
{
public:
  int  m_ProductId;
  DBSTATUS m_IdStatus;
  //.. your other columns, like m_Price, m_Quantity, etc

  BEGIN_COLUMN_MAP(CProduct)
    COLUMN_ENTRY_STATUS(1, m_ProductId, m_IdStatus)
    // your other column could be mapped normally using COLUMN_ENTRY macro
  END_COLUMN_MAP()
  //...
};


Now, before Insert, you need to set the column status to "Ignore", like this:
C++
m_products.m_IdStatus = DBSTATUS_S_IGNORE;
m_products.Insert();


I usually prefer to override the Insert() method in the class. In this case in your CProduct class you would add this:
C++
HRESULT Insert(int nAccessor = 0, bool bGetHRow = false) throw()
{
  m_IdStatus = DBSTATUS_S_IGNORE;

  return __super::Insert(nAccessor, bGetHRow);
}


In this case you do not need to remember to set the status in your code. The class will do it for you.
 
Share this answer
 
Comments
thomas_wingfield 18-Oct-12 7:31am    
Hi Andrew!Thanks for helping.I tried to change from COLUMN_ENTRY_LENGTH_STATUS on COLUMN_ENTRY_STATUS in Id field;Also I set DBSTATUS_S_IGNORE before initializing members for Id but it didn/t help, still DB Error occured
use this function to retrieve the errors (pass the HRESULT returned after you called Insert() method) and post it here:

C++
CString GetDatabaseError( HRESULT hrErr )
{
  CDBErrorInfo ErrorInfo;
  ULONG        cRecords;
  HRESULT      hr;
  ULONG        i;
  CComBSTR     bstrDesc, bstrHelpFile, bstrSource;
  GUID         guid;
  DWORD        dwHelpContext;
  WCHAR        wszGuid[40];
  CString      strError;

  // If the user passed in an HRESULT then trace it
  if (hrErr != S_OK)
  {
    strError.Format(_T("OLE DB Error: 0x%x\n"), hrErr);
  }

  LCID lcLocale = GetSystemDefaultLCID();

  hr = ErrorInfo.GetErrorRecords(&cRecords);
  if (FAILED(hr) && ErrorInfo.m_spErrorInfo == NULL)
  {
    strError += _T("No Error Information found");
  }
  else
  {
    for (i = 0; i < cRecords; i++)
    {
      hr = ErrorInfo.GetAllErrorInfo(i, lcLocale, &bstrDesc, &bstrSource, &guid,
        &dwHelpContext, &bstrHelpFile);
      if (FAILED(hr))
      {
        TRACE(atlTraceDBClient, 0,
          _T("OLE DB Error Record dump retrieval failed: hr = 0x%x\n"), hr);
        return strError;
      }
      StringFromGUID2(guid, wszGuid, sizeof(wszGuid) / sizeof(WCHAR));
      CString strInfo;
#ifdef _DEBUG
      strInfo.Format(_T("Row #: %4d Source: \"%s\" Description: \"%s\" Help File: \"%s\" Help Context: %4d GUID: %s\n"),
        i, static_cast<tchar*>(COLE2T(bstrSource)), static_cast<tchar*>(COLE2T(bstrDesc)), static_cast<tchar*>(COLE2T(bstrHelpFile)), dwHelpContext, static_cast<tchar*>(COLE2T(wszGuid)));
#else
      strInfo.Format(_T("(%4d) Description: \"%s\"\n"), i, static_cast<tchar*>(COLE2T(bstrDesc)));
#endif
      strError += strInfo;
      bstrSource.Empty();
      bstrDesc.Empty();
      bstrHelpFile.Empty();
    }
    TRACE(_T("OLE DB Error Record dump end\n"));
  }
  return strError;
}
 
Share this answer
 
Comments
thomas_wingfield 19-Oct-12 7:10am    
Thank you Andrew:)That's piece of code really helped me.I found error, that was my mistake.Thanks a lot again
chaau 19-Oct-12 8:14am    
you're welcome. this function is from my program. I am using it for error reporting. You can use it in your program as well
thomas_wingfield 19-Oct-12 14:34pm    
thank u).It's great

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900