Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6167415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:30:33+00:00 2026-05-23T22:30:33+00:00

I have a question related with NULL values I am connecting to SQL Server

  • 0

I have a question related with NULL values I am connecting to SQL Server 2008 via ADO following the code:

#include "stdafx.h"
#include <stdio.h>

#include <windows.h>
#include <ole2.h>
#include <oleauto.h>

#ifdef _MSC_VER
#import "c:\Archivos de programa\Archivos comunes\System\ado\msado15.dll" rename ("EOF","adoEOF") no_namespace
#else

#define V_INT(X)         V_UNION(X, intVal)
#define V_UINT(X)        V_UNION(X, uintVal)
#include "msado15.tlh"
#endif

#include <comutil.h>

struct InitOle
{
    InitOle()  { ::CoInitialize(NULL); }
    ~InitOle() { ::CoUninitialize();   }
} InitOle_tag;

//------------------ utility fns to simplify access to recordset fields
_bstr_t RsItem( _RecordsetPtr p, BSTR fldName )
{  // by field name
    return( p->Fields->Item[_variant_t(fldName)]->Value );
}
_bstr_t RsItem( _RecordsetPtr p, long nIdx )
{ // by field # (0 is first)
    return( p->Fields->Item[_variant_t(nIdx)]->Value );
}
//-------------------------------- The Program ----------------
int main()
{
    _RecordsetPtr spRs;
    HRESULT hr;
    _bstr_t sConn= "driver={sql server};SERVER=VIRTUALPC;Database=test;UID=sa; PWD=";
    _bstr_t sSQL= "SELECT att0 FROM [dbo].[mytable]";

    try
    {
        hr= spRs.CreateInstance( __uuidof(Recordset) );
        if FAILED(hr) printf("CreateInstance failed\n");

        hr= spRs->Open( sSQL, sConn, adOpenForwardOnly, adLockReadOnly, adCmdText );
        if FAILED(hr) printf("Open failed\n");

        while( !(spRs->adoEOF) ) {
            printf("%s\n",
                    (char*) RsItem( spRs, 0L )

            );
            spRs->MoveNext();
        }
        spRs->Close();
    } catch( _com_error &e) {
        printf("Error:%s\n",(char*)e.Description());
    }
    return 0;
}

the column I am reading att0 is like:

att0
----
477
113
466
527
NULL
NULL
NULL

After executing the program I get:

477
113
466
527
Error:(null)
Press any key to continue . . .

I would like that when it detects a NULL value the program displays

    477
    113
    466
    527
    -1
    -1
    -1

Any idea, how to accomplish this?

All this works really great, but if in my table (which allows NULLS) I get an error when reading NUll

the main problem is in the section:

while( !(spRs->adoEOF) ) {
            printf("%s\n",
                    (char*) RsItem( spRs, 0L )

            );
            spRs->MoveNext();
        }
  • One more question, Why does the program have error in reading a NULL?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:30:33+00:00Added an answer on May 23, 2026 at 10:30 pm

    I’m sure there is some way to check for null values against the Recordset object but I don’t know how that should be done in c++. But I do know how to fix this in the query instead.

    Change your query to:

    SELECT coalesce(att0, -1) as att0 FROM [dbo].[mytable]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question is related to the command pattern, where we have the following abstraction
I have a question related to how relative paths are interpreted in various environments
I have a simple question related to one-line programming. First an example: function test(a)
Haven't seen many Geneva related questions yet, I have posted this question in the
Although somewhat related to this question , I have what I think is a
Much related to this question , we have a scenario on my team where
This is kind of related to my previous question, but not really. I have
Possible Duplicate: Split string in SQL I have seen a couple of questions related
I have 2 tables (there are more but un related to question) optionValue and
I have question quite much related to this one I asked a while ago

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.