Groups | Blog | Home
all groups > asp.net datagrid control > november 2007 >

asp.net datagrid control : {0:c} causes input string not in correct format exception



Scott M.
11/19/2007 8:43:58 PM
I've seen many posts complaining about this, but none with a solution.....

I have a GridView that is bound to a SQLDataSource and working just fine,
except that when I change the DataFormatString on one of my columns that is
displaying a smallmoney value from SQL (converted to double by .NET
Framework) to {0:c}, I get an Input String Was Not In Correct Format
exception when I attempt to delete a record. Interestgly, editing works
just fine.

I know that the problem is that when the delete takes place, the field
contains something like $299.95, which is clearly not a double or
Interestingly, and thus the error. The 64,000 question is how to fix this
so that when the delete takes place, the data is back in it's original
(unformatted) form. I've tried stripping the "$" and the "," out of the
value in the RowDeleting event handler to no avail.

Any ideas?
Scott M.
11/20/2007 12:22:04 AM
Is this what you want Steven:

<asp:BoundField DataField="RetailPrice"
HeaderText="RetailPrice" SortExpression="RetailPrice"
DataFormatString="{0:c}" />

This does cause my displayed amounts to be formatted as currency, which is a
"catch 22" since now when I try to delete, the value in the bound field is
not the same as it was when it was loaded into the gridview. Now, the value
in this column is no longer compatible with a decimal, double or smallmoney
and so the delete attempt fails.

You mention that you are not seeing your smallmoney SQL amount formatted as
a currency amount, which you should be, so I think you don't have your test
set up correctly.

-Scott


[quoted text, click to view]
stcheng@online.microsoft.com
11/20/2007 4:47:08 AM
Hi Scott,

From your description, you're encountering some "input string incorrect
format ..." error when deleting record in GridView(with DataFormatString
set in column), right?

According to the setting you mentioned, I've performed a simple test with
the following things:


* use a simple SQL Express table with a column(of "smallmoney" type)
* in ASPX page, you SqlDataSource to connect that table(enable edit, delete)
* in GridView, enable Edit,Delete and also apply the following formatstring
to the boundField(of that smallmoney column)

"{0:c}"


============
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True"
ShowEditButton="True" />
<asp:BoundField DataField="id" HeaderText="id"
InsertVisible="False" ReadOnly="True"
SortExpression="id" />
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="price" HeaderText="price"
SortExpression="price" DataFormatString="{0:c}" />
</Columns>
</asp:GridView>
=================

However, the currency symbol doesn't display by default and I can get the
record updated or deleted correctly. Therefore, I think there should be
something different from mine in your page. Would you give me a aspx
template so that I can test against it?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



--------------------
[quoted text, click to view]
stcheng@online.microsoft.com
11/21/2007 9:46:16 AM
Hi Scott,

Here is the aspx template of my test page

price column is the one of smallmoney type in SQL Express:

=======================
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testdbConnectionString %>"
DeleteCommand="DELETE FROM [sm_tb] WHERE [id] = @id"
InsertCommand="INSERT INTO [sm_tb] ([name], [price]) VALUES (@name, @price)"
SelectCommand="SELECT [id], [name], [price] FROM [sm_tb]"
UpdateCommand="UPDATE [sm_tb] SET [name] = @name, [price] = @price WHERE
[id] = @id">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int64" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="name" Type="String" />
<asp:Parameter Name="price" Type="Decimal" />
<asp:Parameter Name="id" Type="Int64" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="name" Type="String" />
<asp:Parameter Name="price" Type="Decimal" />
</InsertParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True"
ShowEditButton="True" />
<asp:BoundField DataField="id" HeaderText="id"
InsertVisible="False" ReadOnly="True"
SortExpression="id" />
<asp:BoundField DataField="name" HeaderText="name"
SortExpression="name" />
<asp:BoundField DataField="price" HeaderText="price"
SortExpression="price" DataFormatString="{0:c}" />
</Columns>
</asp:GridView>

</div>
</form>
===========================

Anything I should changed to match your case?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.










--------------------
[quoted text, click to view]
Scott M.
11/21/2007 11:34:43 PM
Hi Steven,

It looks the same as mine, but if you run it and don't see the smallmoney
data formatted as a currency, then you must have another problem. We can't
get to my question until you can get that far. I didn't do anything special
just bound a GridView to SQL data that has a smallmoney field and chose to
take that bound column and format it as a currency. That part works like a
charm.

-Scott


[quoted text, click to view]
AddThis Social Bookmark Button