Home All Groups Group Topic Archive Search About

Problem with AddNew on SQL server in c++ 2003

Author
16 Dec 2005 9:22 PM
roberta.coffman
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,adCmdTable);
    //Open an IADORecordBinding interface pointer which we'll use for
Binding Recordset to a class
    hr =
pRstEvents->QueryInterface(__uuidof(IADORecordBinding),(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();
    }
}

Author
16 Dec 2005 9:57 PM
Aaron Bertrand [SQL Server MVP]
Try creating a stored procedure and executing that.  Using AddNew from the
middle tier is kinda hokey.


<roberta.coff***@emersonprocess.com> wrote in message
Show quote
news:1134768159.533377.64050@g43g2000cwa.googlegroups.com...
> 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,adCmdTable);
> //Open an IADORecordBinding interface pointer which we'll use for
> Binding Recordset to a class
> hr =
> pRstEvents->QueryInterface(__uuidof(IADORecordBinding),(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();
> }
> }
>
Author
16 Dec 2005 11:15 PM
markc600
I'm not sure binding to the CString Category works here, also
sizeof(Category) is pretty meaningless. Suggest you change
CString to a fixed sized array to mirror the size of the column
in your table, i.e. change
  CString Category;
to
  CHAR   Category[80];
Author
27 Feb 2006 7:49 PM
CWang
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

Show quote
"roberta.coff***@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,adCmdTable);
>     //Open an IADORecordBinding interface pointer which we'll use for
> Binding Recordset to a class
>     hr =
> pRstEvents->QueryInterface(__uuidof(IADORecordBinding),(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();
>     }
> }
>
>
Author
27 Feb 2006 11:53 PM
John Linville
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


Show quote
"CWang" <CW***@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.coff***@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,adCmdTable);
>> //Open an IADORecordBinding interface pointer which we'll use for
>> Binding Recordset to a class
>> hr =
>> pRstEvents->QueryInterface(__uuidof(IADORecordBinding),(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();
>> }
>> }
>>
>>

AddThis Social Bookmark Button