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

Discussion in 'ASP .Net' started by Sam, Jun 16, 2006.

  1. Sam

    Sam Guest

    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>
     
    Sam, Jun 16, 2006
    #1
    1. Advertising

  2. Sam

    Prnda76

    Joined:
    Feb 11, 2010
    Messages:
    1
    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: Feb 11, 2010
    Prnda76, Feb 11, 2010
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Artek
    Replies:
    3
    Views:
    2,780
    Steven Cheng[MSFT]
    Feb 14, 2004
  2. shannon
    Replies:
    1
    Views:
    8,631
  3. shannon
    Replies:
    0
    Views:
    586
    shannon
    Feb 1, 2006
  4. Sam
    Replies:
    0
    Views:
    211
  5. Guy Hocking

    Displaying data to 2 decimal points

    Guy Hocking, Mar 7, 2004, in forum: ASP General
    Replies:
    8
    Views:
    234
    Bob Barrows
    Mar 7, 2004
Loading...

Share This Page