mfc >> CRecordset::GetFieldValue and incorrect string length.

by Simon Palmer » Mon, 31 Jan 2005 12:13:16 GMT

Howdy,

I'm having an issue with using CRecordset::GetFieldValue to access a
column of floats in a SQL Server database. i'm using Visual Studio .net
2003.

The problem is the strings that are being returned are all 1 character
too long.
For example GetFieldValue returns the cstring "93.99999999999999",
which is 17 chars long, but if i call GetLength on the cstring it says
its 18 characters long.

When i stepped through CRecordset::GetFieldValueEx to see what was
going on, i noticed that getfieldvalueex, while setting the string
value, calls CString::GetBufferSetLength but has no corresponding
CString::releasebuffer.

So i've changed my code to something like this

CString sVal;
while (!rs.IsEOF())
{
rs.GetFieldValue((short) 0, sVal);
sVal.ReleaseBuffer();
saLookups.Add(sVal);
}

and now all my strings are the right length.

So am i doing something fundamentally wrong in how i'm getting the
values from the database? or is this what i'm supposed to do?



Similar Threads

1. buffer to string: incorrect string length

I am trying to convert a buffer to a string using
Encoding.UTF8.GetString(...)
where the buffer contains trailing '\0' s.

When I get the length of the string or print the string the '\0' s are
included as spaces. Why doesn't a string use '\0' to terminate a
string?

example:

byte[] buffer = new buffer[10];
buffer[0] = (byte)'1';
buffer[1] = (byte)'2';
buffer[2] = (byte)'3';
buffer[3] = 0;

string text = Encoding.UTF8.GetString(buffer);

Console.Write('|');
Console.Write(text);
Console.Write('|');

The output is: "|123       |"

actually I am getting the buffer reading from a socket and the buffer
is null terminated and then the rest of the buffer is filled with
junk. When the buffer is converted to a string and printed the text
and the junk are printed.
So I guess the question might be, how do I terminate a string?

thanks for the help

John

2. CRecordset::GetFieldValue and SQLServer 2000

3. CRecordSet::GetFieldValue creates unhandled exception

Hi all,
I have a problem with CRecordSet method:
void GetFieldValue( LPCTSTR lpszName, CString& strValue );
When I use it with the field name, e.g.:
CString cstrValue;
rs.GetFieldValue ("data",cstrValue);
I receive Unhandled exception in KERNEL32.dll, whereas if I use
GetFieldValue with the field index, everything works fine.
Any ideas why?
Thanks,
Anat.

4. CRecordset::GetFieldValue function returns wrong value

5. CRecordset:GetFieldValue CMemoryException thrown only in Vista

6. why CRecordset::GetFieldValue() error????

7. BUG in CRecordset::GetFieldValue with CStringW

Please confirm that I have correctly identified a bug in the MFC source.  It is more specifically in 
the GetLongCharDataAndCleanup function, when filling a CStringW variable.  After calling GetData, 
the nActualSize returned is the number of bytes occupied by the string data. However, because this 
is a Unicode string, the character count should be half that.  But, when GetLongCharDataAndCleanup 
is called, it sets the string length to the value of nActualSize.  This causes the CStringW variable 
to think that it has twice as many characters than it does!  If you subsequently do a += or Append 
operation on the string, it puts the appended string in the midst of garbage beyond the end of the 
actual string, because it thinks the string is twice as long.

Either this is a bug in the MFC source, or I am using something incorrectly.


8. Reading "ntext" field with CRecordset::GetFieldValue