ASP.NET FormView Money Data Type Binding Problem with 2 Decimal Points..

S

Sam

I am trying to use a Simple form with 3 fields from SQL NorthWind
Database (Order Details Table with 3 Fields. - OrderId, ProductId and
Unit Price). The Field Unit Price has a data type of 'Money Type'.

All I want is to able to edit/view/insert 'UnitPrice' field with 2
decimal points instead of 4 decimal points.

If I use: Bind("UnitPrice", "{0:c}") in Edit Template, I got: "Input
string was not in a correct format...". Although this works in Item
Template....

I know I can do it easily in MS Access ADP Form where I set the Decimal
Places 'Auto'. It works perfectly without any problem with the same SQL
Datasource.

I have been trying to use the same ADP logic in .net 2.0 Form View. No
luck.....

Below is my complete code. Any help/suggestions will be appreciated...
I am also new to .net Environment....

- Sam

_________________

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html>
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
<br />
<asp:FormView ID="FormView1" runat="server" AllowPaging="True"
DataKeyNames="OrderID,ProductID"
DataSourceID="SqlDataSource1" Width="256px">
<EditItemTemplate>
OrderID:
<asp:Label ID="OrderIDLabel1" runat="server" Text='<%#
Eval("OrderID") %>'></asp:Label><br />
UnitPrice:
<asp:TextBox ID="UnitPriceTextBox" runat="server"
Text='<%# Bind("UnitPrice") %>'></asp:TextBox><br />
ProductID:
<asp:Label ID="ProductIDLabel1" runat="server"
Text='<%# Eval("ProductID") %>'></asp:Label><br />
<asp:LinkButton ID="UpdateButton" runat="server"
CausesValidation="True" CommandName="Update"
Text="Update">
</asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel">
</asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
OrderID:
<asp:TextBox ID="OrderIDTextBox" runat="server"
Text='<%# Bind("OrderID") %>'>
</asp:TextBox><br />
UnitPrice:
<asp:TextBox ID="UnitPriceTextBox" runat="server"
Text='<%# Bind("UnitPrice") %>'>
</asp:TextBox><br />
ProductID:
<asp:TextBox ID="ProductIDTextBox" runat="server"
Text='<%# Bind("ProductID") %>'>
</asp:TextBox><br />
<asp:LinkButton ID="InsertButton" runat="server"
CausesValidation="True" CommandName="Insert"
Text="Insert">
</asp:LinkButton>
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel">
</asp:LinkButton>
</InsertItemTemplate>
<ItemTemplate>
OrderID:
<asp:Label ID="OrderIDLabel" runat="server" Text='<%#
Eval("OrderID") %>'></asp:Label><br />
UnitPrice:
<asp:Label ID="UnitPriceLabel" runat="server" Text='<%#
Bind("UnitPrice") %>'></asp:Label><br />
ProductID:
<asp:Label ID="ProductIDLabel" runat="server" Text='<%#
Eval("ProductID") %>'></asp:Label><br />
<asp:LinkButton ID="EditButton" runat="server"
CausesValidation="False" CommandName="Edit"
Text="Edit"></asp:LinkButton>
<asp:LinkButton ID="DeleteButton" runat="server"
CausesValidation="False" CommandName="Delete"
Text="Delete"></asp:LinkButton>
<asp:LinkButton ID="NewButton" runat="server"
CausesValidation="False" CommandName="New"
Text="New"></asp:LinkButton>
</ItemTemplate>
</asp:FormView>
<br />
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString1 %>"
DeleteCommand="DELETE FROM [Order Details] WHERE [OrderID]
= @OrderID AND [ProductID] = @ProductID"
InsertCommand="INSERT INTO [Order Details] ([OrderID],
[UnitPrice], [ProductID]) VALUES (@OrderID, @UnitPrice, @ProductID)"
SelectCommand="SELECT [OrderID], [UnitPrice], [ProductID]
FROM [Order Details]"
UpdateCommand="UPDATE [Order Details] SET [UnitPrice] =
@UnitPrice WHERE [OrderID] = @OrderID AND [ProductID] = @ProductID">
<DeleteParameters>
<asp:parameter Name="OrderID" Type="Int32" />
<asp:parameter Name="ProductID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="UnitPrice" Type="Decimal" />
<asp:parameter Name="OrderID" Type="Int32" />
<asp:parameter Name="ProductID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:parameter Name="OrderID" Type="Int32" />
<asp:parameter Name="UnitPrice" Type="Decimal" />
<asp:parameter Name="ProductID" Type="Int32" />
</InsertParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
 
Joined
Feb 11, 2010
Messages
1
Reaction score
0
Hope this helps...

Hi buddy,

what you need in your edit template is the following:

Bind("UnitPrice", "{0:0.00}")

Additionally you will need the following line in the Update Parameters segment of your data source (in your case SqlDataSource):

<asp:parameter Name="UnitPrice" ConvertEmptyStringToNull="true" DbType="Decimal" />

This will solve your issue exactly the way you want it...
 
Last edited:

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

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top