Click here to Skip to main content
15,074,987 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my front-end code
<td><asp:Label ID="lblPPrice" runat="server" Text='<%# Eval("PPrice","{0:c}")
   %>' ></asp:Label>
                <asp:TextBox ID="txtPPrice" runat="server" Text='<%# Eval("PPrice","
   {0:c}") %>' Visible="false"></asp:TextBox> </td>

                 <td><asp:Label ID="lblPSelPrice" runat="server" Text='<%#
   Eval("PSelPrice","{0:c}") %>' ></asp:Label>
                <asp:TextBox ID="txtPSelPrice" runat="server" Text='<%#
   Eval("PSelPrice","{0:c}") %>' Visible="false"></asp:TextBox> </td>

This is my code behind

cmd.Parameters.AddWithValue("@PPrice", SqlDbType.NVarChar).Value =
   double.TryParse(productPrice, out money);
               cmd.Parameters.AddWithValue("@PSelPrice", SqlDbType.NVarChar).Value=
   double.TryParse(productSelPrice, out money);

What I have tried:

I have tried to change from double to float and char but same thing happen. Even when I update quantity, the price part change to 0.00 . How to fix this issue?
Updated 31-May-21 16:35pm

1 solution

Have a look at the definition for Double.TryParse Method (System) | Microsoft Docs[^] .
The TryParse method returns a boolean indicating if parsing was successful or not. The actual parsed value is returned using the second, out parameter.

So instead of using the return value from TryParse, you should have something like the example below.

Also note that the AddWithValue method has the value as second parameter so you should use that instead of the value property of the created parameter. Now you're passing the type as value. For more info, please see SqlParameterCollection.AddWithValue(String, Object) Method (System.Data.SqlClient) | Microsoft Docs[^]

if (!double.TryParse(productPrice, out double convertedPrice)) {
   // the number was in incorrect format, inform the user etc...
if (!double.TryParse(productSelPrice, out double convertedSelPrice)) {
   // the number was in incorrect format, inform the user etc...
cmd.Parameters.AddWithValue("@PPrice", convertedPrice;
cmd.Parameters.AddWithValue("@PSelPrice", convertedSelPrice);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900