PC Review


Reply
Thread Tools Rate Thread

ADO or String.Format bug ? ......

 
 
Nicola Cisternino
Guest
Posts: n/a
 
      24th Jun 2005
Hi
It seems that String.Format method, when use a ***ZERO VALUE*** object
coming from a SqlDataAdapter Fill method works fine, while if the same
query is runned using an SqlDataReader ExecuteReader method, the
formatted result is wrong ....
For my tests i've used the Sql Server 2000 Northwind database and i've
updated the Orders.Freight Column to zero (UPDATE Orders SET Freight=0).
My general objective is to format all significative decimal values
(value > 0)using a mask: #####0.00 and format all zero values using
another mask: #####.##.
This is obtained with:
String.Format("{0:#####0.00;-#####0.00;######.##}", VALUE-TO-FORMAT)
The VALUE-TO-FORMAT is obtained either using a server-side cursor
(DataReader) and a client-side cursor (DataTable).
In the first case it display ",00" (!!! ???) ....
.... while using a DataAdapter it (correctly) display "" .....

The complete VB.net code is the following:
=============================================================================
Dim Cn As New SqlClient.SqlConnection("uid=sa; pwd=;
database=northwind; server=xdev99")
Dim Cmd As New SqlClient.SqlCommand("SELECT Freight FROM
orders", Cn)
Cn.Open()
Dim Dr As SqlClient.SqlDataReader = Cmd.ExecuteReader
If Dr.Read Then

Console.WriteLine(String.Format("{0:#####0.00;-#####0.00;######.##}",
Dr.Item("Freight")))
End If
Dr.Close()

Dim Dt As New DataTable
Dim Da As New SqlClient.SqlDataAdapter("SELECT 0 FROM orders", Cn)
Da.Fill(Dt)
If Dt.Rows.Count > 0 Then

Console.WriteLine(String.Format("{0:#####0.00;-#####0.00;######.##}",
Dt.Rows(0).Item("Freight")))
End If

Cn.Close()
==============================================================================
What's your idea ?
Thanks.
Nicola.
 
Reply With Quote
 
 
 
 
Marina
Guest
Posts: n/a
 
      24th Jun 2005
Your queries are different. The datareader gets a 'select Freigh from
orders', your adapter gets a 'select 0 from orders'. This is not a clean
experiment, as it is not clear exactly what the query is retrieving.

"Nicola Cisternino" <(E-Mail Removed)> wrote in message
news:%23vM%(E-Mail Removed)...
> Hi
> It seems that String.Format method, when use a ***ZERO VALUE*** object
> coming from a SqlDataAdapter Fill method works fine, while if the same
> query is runned using an SqlDataReader ExecuteReader method, the formatted
> result is wrong ....
> For my tests i've used the Sql Server 2000 Northwind database and i've
> updated the Orders.Freight Column to zero (UPDATE Orders SET Freight=0).
> My general objective is to format all significative decimal values (value
> > 0)using a mask: #####0.00 and format all zero values using another mask:

> #####.##.
> This is obtained with:
> String.Format("{0:#####0.00;-#####0.00;######.##}", VALUE-TO-FORMAT)
> The VALUE-TO-FORMAT is obtained either using a server-side cursor
> (DataReader) and a client-side cursor (DataTable).
> In the first case it display ",00" (!!! ???) ....
> ... while using a DataAdapter it (correctly) display "" .....
>
> The complete VB.net code is the following:
> =============================================================================
> Dim Cn As New SqlClient.SqlConnection("uid=sa; pwd=;
> database=northwind; server=xdev99")
> Dim Cmd As New SqlClient.SqlCommand("SELECT Freight FROM orders",
> Cn)
> Cn.Open()
> Dim Dr As SqlClient.SqlDataReader = Cmd.ExecuteReader
> If Dr.Read Then
>
> Console.WriteLine(String.Format("{0:#####0.00;-#####0.00;######.##}",
> Dr.Item("Freight")))
> End If
> Dr.Close()
>
> Dim Dt As New DataTable
> Dim Da As New SqlClient.SqlDataAdapter("SELECT 0 FROM orders", Cn)
> Da.Fill(Dt)
> If Dt.Rows.Count > 0 Then
>
> Console.WriteLine(String.Format("{0:#####0.00;-#####0.00;######.##}",
> Dt.Rows(0).Item("Freight")))
> End If
>
> Cn.Close()
> ==============================================================================
> What's your idea ?
> Thanks.
> Nicola.



 
Reply With Quote
 
Nicola Cisternino
Guest
Posts: n/a
 
      24th Jun 2005
Sorry, i've posted the wrong example ..... in my real tests the queries
are absolutely identical !!! (SELECT Freight FROM orders)

Thanks.

Marina wrote:
> Your queries are different. The datareader gets a 'select Freigh from
> orders', your adapter gets a 'select 0 from orders'. This is not a clean
> experiment, as it is not clear exactly what the query is retrieving.
>
> "Nicola Cisternino" <(E-Mail Removed)> wrote in message
> news:%23vM%(E-Mail Removed)...
>
>>Hi
>>It seems that String.Format method, when use a ***ZERO VALUE*** object
>>coming from a SqlDataAdapter Fill method works fine, while if the same
>>query is runned using an SqlDataReader ExecuteReader method, the formatted
>>result is wrong ....
>>For my tests i've used the Sql Server 2000 Northwind database and i've
>>updated the Orders.Freight Column to zero (UPDATE Orders SET Freight=0).
>>My general objective is to format all significative decimal values (value
>> > 0)using a mask: #####0.00 and format all zero values using another mask:

>>#####.##.
>>This is obtained with:
>>String.Format("{0:#####0.00;-#####0.00;######.##}", VALUE-TO-FORMAT)
>>The VALUE-TO-FORMAT is obtained either using a server-side cursor
>>(DataReader) and a client-side cursor (DataTable).
>>In the first case it display ",00" (!!! ???) ....
>>... while using a DataAdapter it (correctly) display "" .....
>>
>>The complete VB.net code is the following:
>>=============================================================================
>> Dim Cn As New SqlClient.SqlConnection("uid=sa; pwd=;
>>database=northwind; server=xdev99")
>> Dim Cmd As New SqlClient.SqlCommand("SELECT Freight FROM orders",
>>Cn)
>> Cn.Open()
>> Dim Dr As SqlClient.SqlDataReader = Cmd.ExecuteReader
>> If Dr.Read Then
>>
>>Console.WriteLine(String.Format("{0:#####0.00;-#####0.00;######.##}",
>>Dr.Item("Freight")))
>> End If
>> Dr.Close()
>>
>> Dim Dt As New DataTable
>> Dim Da As New SqlClient.SqlDataAdapter("SELECT 0 FROM orders", Cn)
>> Da.Fill(Dt)
>> If Dt.Rows.Count > 0 Then
>>
>>Console.WriteLine(String.Format("{0:#####0.00;-#####0.00;######.##}",
>>Dt.Rows(0).Item("Freight")))
>> End If
>>
>> Cn.Close()
>>==============================================================================
>>What's your idea ?
>>Thanks.
>>Nicola.

>
>
>

 
Reply With Quote
 
Nicola Cisternino
Guest
Posts: n/a
 
      24th Jun 2005
I've also noticed that the problem occurs only using money and
smallmoney SQL types ....

Nicola Cisternino wrote:
> Hi
> It seems that String.Format method, when use a ***ZERO VALUE*** object
> coming from a SqlDataAdapter Fill method works fine, while if the same
> query is runned using an SqlDataReader ExecuteReader method, the
> formatted result is wrong ....
> For my tests i've used the Sql Server 2000 Northwind database and i've
> updated the Orders.Freight Column to zero (UPDATE Orders SET Freight=0).
> My general objective is to format all significative decimal values
> (value > 0)using a mask: #####0.00 and format all zero values using
> another mask: #####.##.
> This is obtained with:
> String.Format("{0:#####0.00;-#####0.00;######.##}", VALUE-TO-FORMAT)
> The VALUE-TO-FORMAT is obtained either using a server-side cursor
> (DataReader) and a client-side cursor (DataTable).
> In the first case it display ",00" (!!! ???) ....
> ... while using a DataAdapter it (correctly) display "" .....
>
> The complete VB.net code is the following:
> =============================================================================
>
> Dim Cn As New SqlClient.SqlConnection("uid=sa; pwd=;
> database=northwind; server=xdev99")
> Dim Cmd As New SqlClient.SqlCommand("SELECT Freight FROM
> orders", Cn)
> Cn.Open()
> Dim Dr As SqlClient.SqlDataReader = Cmd.ExecuteReader
> If Dr.Read Then
>
> Console.WriteLine(String.Format("{0:#####0.00;-#####0.00;######.##}",
> Dr.Item("Freight")))
> End If
> Dr.Close()
>
> Dim Dt As New DataTable
> Dim Da As New SqlClient.SqlDataAdapter("SELECT 0 FROM orders", Cn)
> Da.Fill(Dt)
> If Dt.Rows.Count > 0 Then
>
> Console.WriteLine(String.Format("{0:#####0.00;-#####0.00;######.##}",
> Dt.Rows(0).Item("Freight")))
> End If
>
> Cn.Close()
> ==============================================================================
>
> What's your idea ?
> Thanks.
> Nicola.

 
Reply With Quote
 
David Browne
Guest
Posts: n/a
 
      25th Jun 2005

"Nicola Cisternino" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I've also noticed that the problem occurs only using money and smallmoney
> SQL types ....
>
> Nicola Cisternino wrote:
>> Hi
>> It seems that String.Format method, when use a ***ZERO VALUE*** object
>> coming from a SqlDataAdapter Fill method works fine, while if the same
>> query is runned using an SqlDataReader ExecuteReader method, the
>> formatted result is wrong ....
>> For my tests i've used the Sql Server 2000 Northwind database and i've
>> updated the Orders.Freight Column to zero (UPDATE Orders SET Freight=0).
>> My general objective is to format all significative decimal values (value
>> > 0)using a mask: #####0.00 and format all zero values using another

>> mask: #####.##.
>> This is obtained with:
>> String.Format("{0:#####0.00;-#####0.00;######.##}", VALUE-TO-FORMAT)
>> The VALUE-TO-FORMAT is obtained either using a server-side cursor
>> (DataReader) and a client-side cursor (DataTable).
>> In the first case it display ",00" (!!! ???) ....
>> ... while using a DataAdapter it (correctly) display "" .....
>>
>> The complete VB.net code is the following:
>> =============================================================================
>> Dim Cn As New SqlClient.SqlConnection("uid=sa; pwd=; database=northwind;
>> server=xdev99")
>> Dim Cmd As New SqlClient.SqlCommand("SELECT Freight FROM orders",
>> Cn)
>> Cn.Open()
>> Dim Dr As SqlClient.SqlDataReader = Cmd.ExecuteReader
>> If Dr.Read Then
>>
>> Console.WriteLine(String.Format("{0:#####0.00;-#####0.00;######.##}",
>> Dr.Item("Freight")))
>> End If
>> Dr.Close()
>>
>> Dim Dt As New DataTable
>> Dim Da As New SqlClient.SqlDataAdapter("SELECT 0 FROM orders",
>> Cn)
>> Da.Fill(Dt)
>> If Dt.Rows.Count > 0 Then
>>
>> Console.WriteLine(String.Format("{0:#####0.00;-#####0.00;######.##}",
>> Dt.Rows(0).Item("Freight")))
>> End If
>>
>> Cn.Close()
>> ==============================================================================



In the DataTable, the value will already have been converted to a .NET type
(System.Decimal probably).

In the DataReader the type is returned as a raw SQL Server type eg
(System.Data.SqlTypes.SqlMoney). String.Format knows things about
System.Decimal, which it doesn't about System.Data.SqlTypes.SqlMoney. In
particular Decimal implements IFormattable, IComparable, and IConvertible,
which help control formatting.

In addition to being another in the long list of why not to use DataReaders,
you can work around this by setting the value to a local variable before
passing it to String.Format.

Dim freight as Decimal = Dr.Item("Freight")
....

David


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make string align by string.Format() in CJK? Dancefire Microsoft Dot NET Framework 4 15th Nov 2007 05:48 PM
Convert a latebinded DataTable column's format from DateTime to string (or format the date time value) RSH Microsoft VB .NET 0 6th Dec 2006 03:49 PM
Exception in String.Format "Input string was not in a correct format" The Crow Microsoft C# .NET 1 23rd Sep 2005 11:04 AM
How do I use a curly brace within a string passed to String.Format? Jason Kendall Microsoft VB .NET 4 20th May 2004 02:25 AM
connection string exception : Format of the initialization string does not conform to specification starting at index 0 Gaurav Microsoft ASP .NET 0 7th Nov 2003 10:24 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:37 AM.