CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2005
    Posts
    151

    ADO : error reading an empty field

    Hello,

    I'm using ADO and when I try to load an empty field with the following code, my application crashes.

    Code:
    m_strName		= (char*) (_bstr_t) m_pRS->GetCollect("Name") ;
    How can I fix this "little" problem?

    Thanx in advance for any help

  2. #2
    Join Date
    May 2004
    Location
    London, England
    Posts
    563

    Re: ADO : error reading an empty field

    At first glance I see nothing wrong with your code, nor should you have any problems reading in an empty field, otherwise they'll be a lot of databases around the planet just falling over.. So it must be something else, like an invalid column name.

    Have you put your code in try... catch(...) blocks? If you haven't its strongly advised that you do. You should have the following set up:

    Code:
    try
    {
    ... 
    }
    catch(_com_error &e)
    {
    // find errors here
    }
    // and for added security I add another catch to find out exactly whats happened:
    catch(...)
    {
    	HRESULT hr = (HRESULT) 0L;
    	ErrorsPtr pErrors = NULL;
    	ErrorPtr  pError = NULL;
    	int nErrorCode = -1;
    
    	pErrors =m_ptrConnection->GetErrors();
    
    	int nCount = pErrors->GetCount();
    
                    for( long i = 0; (!FAILED(hr)) && (i < nCount); i++ )
                   {
    		TRACE( "\t Dumping ADO Error %d of %d", i+1, nCount );
    
    		hr = pErrors->get_Item((_variant_t)((long)i), &pError );
    
    		_bstr_t bstrSource     ( pError->GetSource()      );
    		_bstr_t bstrDescription( pError->GetDescription() );
    		_bstr_t bstrHelpFile   ( pError->GetHelpFile()    );
    		_bstr_t bstrSQLState   ( pError->GetSQLState()    );
    	
    		TRACE( "\t\t Number      = %ld\n", pError->GetNumber()       );
    		TRACE( "\t\t Source      = %s\n",  (LPCTSTR) bstrSource      );
    		TRACE( "\t\t Description = %s\n",  (LPCTSTR) bstrDescription );
    		TRACE( "\t\t HelpFile    = %s\n",  (LPCTSTR) bstrHelpFile    );
    		TRACE( "\t\t HelpContext = %ld\n", pError->GetHelpContext()  );
    		TRACE( "\t\t SQLState    = %s\n",  (LPCTSTR) bstrSQLState    );
    		TRACE( "\t\t HelpContext = %ld\n", pError->GetHelpContext()  );
    		TRACE( "\t\t NativeError = %ld\n", pError->GetNativeError()  );
    		nErrorCode = pError->GetHelpContext();
        }
    }
    The last catch will tell you exactly whats happened.

    I also couldn't help noticing the great lengths you go to convert the string into a CString but I would knock all that on the head and keep it simple :
    Code:
    CString strResults(m_pRS->GetCollect("Name"));
    and then just assign it to your member var if you wish.

    Hope that has helped.

    Regards

    John
    Last edited by Vaderman; July 26th, 2005 at 04:08 PM. Reason: Necessary conversion??
    I don't mind that you think slowly but I do mind that you are publishing faster than you think. Wolfgang Pauli, physicist, Nobel laureate (1900-1958)

  3. #3
    Join Date
    Oct 1999
    Location
    ks
    Posts
    523

    Re: ADO : error reading an empty field

    i ran into the same problem and could not find away around it other than populating the field with a character that the app would not use.

    the record set ado creates somehow does not like getting queried for a null value.

    j

  4. #4
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Lightbulb Re: ADO : error reading an empty field

    Quote Originally Posted by da_cobra
    Hello,

    I'm using ADO and when I try to load an empty field with the following code, my application crashes.

    Code:
    m_strName		= (char*) (_bstr_t) m_pRS->GetCollect("Name") ;
    How can I fix this "little" problem?

    Thanx in advance for any help

    In case of BSTR or _bstr_t check if lenght is 0 place a Empty string or Initialize it with something.otherwise it will give you error

  5. #5
    Join Date
    Apr 2005
    Posts
    151

    Re: ADO : error reading an empty field

    uhm, I'm a bit new to VC++, can you give me some example code pls?

    thanx in advance

  6. #6
    Join Date
    Apr 2003
    Location
    Athens, Greece
    Posts
    1,094

    Re: ADO : error reading an empty field

    Your code should have a problem with NULL values when you perform the casting...
    You may try something like this:
    Code:
    variant_t vtName = m_pRS->GetCollect("Name") ;
    m_strName = (vtName.vt == VT_NULL) ? "" : (char*) (_bstr_t) vtName;
    That's if you want to put into the m_strName a "" when finding a NULL.
    Extreme situations require extreme measures

  7. #7
    Join Date
    Apr 2005
    Posts
    151

    Re: ADO : error reading an empty field

    thx, it works great with your code now

    every1 thx for the help!

  8. #8
    Join Date
    Apr 2001
    Location
    Tampa Florida
    Posts
    233

    Thumbs up Re: ADO : error reading an empty field

    panayotisk

    I had the same problem in ADO with CString conversions.
    The problem is with the variant access if null.

    your code example
    Code:
    m_strName = (vtName.vt == VT_NULL) ? "" : (char*) (_bstr_t) vtName;
    works fine now that i have used the .vt instead of the bstrVal that Did Not work.

    Code:
    vI.bstrVal != VT_NULL
    thanks
    "trampling out the vintage"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured