Friday, March 23, 2012

Problem with AddNew on SQL server in c++ 2003

I encounter the similar problem, which cannot add new record into an empty
table. If the table is not empty, it is OK. Any one has clue for this proble
m?
Thanks!
Cwang
"roberta.coffman@.emersonprocess.com" wrote:

> Hello,
> I created a very simple database with only one table (Records) and on
> that table only one column (Category datatype nvarchar).
> I am trying to use the AddNew ado example found on msnd but it always
> inserts a null value instead of the value I am trying to insert.
> All my HRESULTs say S_OK but the CategoryStatus is alway 3 (which is
> null).
> I am using UNICODE.
> I can insert records just fine if I use the INSERT INTO command but I
> am not having any luck with the AddNew API.
> Can anyone help?
> Here is my code:
> class CJournalRecord :public CADORecordBinding
> {
> BEGIN_ADO_BINDING(CJournalRecord)
> ADO_VARIABLE_LENGTH_ENTRY2(1, adVarChar, Category, sizeof(Category),
> CategoryStatus, TRUE)
> END_ADO_BINDING()
> public:
> CString Category;
> ULONG CategoryStatus;
> };
> HRESULT hr = S_OK;
> _RecordsetPtr pRstEvents = NULL;
> IADORecordBinding *picRs = NULL;
> hr = pRstEvents.CreateInstance(__uuidof(Recordset));
> if (hr != S_OK)
> {
> }
> else
> {
> //the connection is already open
> CJournalRecord newEvent;
> hr = pRstEvents->Open(_T("Records"),_variant_t((IDispatch *)
> connection, true),adOpenKeyset,adLockOptimistic,adCm
dTable);
> //Open an IADORecordBinding interface pointer which we'll use for
> Binding Recordset to a class
> hr =
> pRstEvents-> QueryInterface(__uuidof(IADORecordBindin
g),(LPVOID*)&picRs);
> hr = picRs->BindToRecordset(&newEvent);
> newEvent.Category = _T("SeeYou");
> newEvent.CategoryStatus = adFldNull;
> if(hr!=S_OK)
> {
> }
> else
> {
> hr = picRs->AddNew(&newEvent);
> int status = (int)newEvent.CategoryStatus;
> //hr = pRstEvents->Update();
> }
> }
>Hi
My problem was when the recordset was instantated by a strored Proc or a
oCmdLExecute it was read only and would not Update() or Addnew() at anytime.
I instantate the RS using a text string now
When wise men disapprove, that's bad;
when fools applaud, that's worse.
A Spanish proverb
John Linville
"CWang" <CWang@.discussions.microsoft.com> wrote in message
news:F60BE31E-F792-4B7F-9AC9-FD3B9EAD563D@.microsoft.com...
>I encounter the similar problem, which cannot add new record into an empty
> table. If the table is not empty, it is OK. Any one has clue for this
> problem?
> Thanks!
> Cwang
> "roberta.coffman@.emersonprocess.com" wrote:
>

No comments:

Post a Comment