VB.NET CStr Function Yields Different Results With SQL Decimal Value

B

Bill L.

We recently noticed that the vb.net CStr function yields different
results than the vb6 version when converting SQL decimal data. If, for
example, the data is defined in SQL as decimal(19,10), the vb.net CStr
function will return ten digits to the right of the decimal,
regardless of the data value. In this case, if the data value is 5,
the vb.net CStr function will return 5.0000000000. The vb6 CStr
function will simply return 5. In other words, the vb.net CStr
function always returns digits the length of the scale value after the
decimal. The vb6 CStr function will not return the trailing zeroes.
Why?

Thanks, Bill
 
C

CJ Taylor

Try using ToString() instead. It's inherited from Object. CStr is actually
using the Microsoft.VisualBasic library which may or may not cause problems.
ToString() is the *correct* .NET way to do it (you cuould also use CType,
but ToString just makes it easier in most cases)

HTH,
CJ
 
P

Phill. W

Bill L. said:
We recently noticed that the vb.net CStr function yields different
results than the vb6 version when converting SQL decimal data.

Bill,

I gave up on VB4's CStr() function many, /many/ moons ago, being
too lazy to have to thank about the leading space that positive values
are tagged with (reserved for a minus sign on nagative numbers, of
course). I /invariably/ used Format() to convert numbers into Strings
which, in .Net, means <value>.ToString( <format> ).

HTH,
Phill W.
 
C

Cor Ligthert

Hi CJ,

I see I am in the opposition toDay.

toString() is the from C languages derived method to do it.

(The only one I use by the way)

Cor
 
C

CJ Taylor

I never said I was 100% correct. =)

BTW, have you read some of the conversations from msmobiles in the
smartphone group.

Now that is some funny stuff. He bashes the EU alot... Not that its funny
he's bashing the EU, but his "evidence" supporting his theories is funny.

=)
 
C

Cor Ligthert

BTW, have you read some of the conversations from msmobiles in the
smartphone group.

Now that is some funny stuff. He bashes the EU alot... Not that its funny
he's bashing the EU, but his "evidence" supporting his theories is funny.
I was looking, however was only suprissed that Bill Vaughn was involved in
that newsgroup for the rest nothing intresting. Did not see that on the EU,
however for me it seems to be a kid.

I wish often the times that especially Nak was doing those things but much
better in this newsgroup where there again.

Cor
 
H

Herfried K. Wagner [MVP]

* "Phill. W said:
I gave up on VB4's CStr() function many, /many/ moons ago, being
too lazy to have to thank about the leading space that positive values
are tagged with (reserved for a minus sign on nagative numbers, of

I think you are talking about the 'Str' function, not 'CStr'.
 
H

Herfried K. Wagner [MVP]

* "CJ Taylor said:
ToString() is the *correct* .NET way to do it (you cuould also use CType,
but ToString just makes it easier in most cases)

There is no "correct .NET way", there is only personal preference.
 
B

Bill Logan

As it turns out, I am able to use ToString instead of CStr if I
explicitly convert the data value to Object using CType(I don't
necessarily know what the data type is being returned from SQL, I'm just
blindly converting to String). Thanks for the suggestion. This is, of
course, preferable.

The behavior is, however, the same. To see for yourself, return from SQL
a value of decimal data type and simply convert it to string.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top