?
Solved

Error inserting or updating currency in ASP .NET 2.0 to an SQL procedure.

Posted on 2006-04-08
5
Medium Priority
?
398 Views
Last Modified: 2011-09-20
What is the roundabout handling currency in asp textbox saving to sql server?
- I am updating to a stored procedure.
- asp control: Formview
- asp code:
            <UpdateParameters>
                <asp:Parameter Name="TaskID" Type="Int32" />
                <asp:Parameter Name="JOBID" Type="Int32" />
                <asp:Parameter Name="TechID" Type="Int32" />
                <asp:Parameter Name="EmployeeID" Type="Int32" />
                <asp:Parameter Name="TaskCodeID" Type="Int32" />
                <asp:Parameter Name="TaskDate" Type="DateTime" />
                <asp:Parameter Name="StartTime" Type="DateTime" />
                <asp:Parameter Name="Notes" Type="String" />
                <asp:Parameter Name="EndTime" Type="DateTime" />
***          This is the problem field:
                <asp:Parameter Name="RateCharged" Type="Decimal" />
***
                <asp:Parameter Name="Invoiced" Type="Boolean" />
                <asp:Parameter Name="Completed" Type="Boolean" />
            </UpdateParameters>

- asp textbox:
                    <asp:TextBox ID="RateChargedTextBox" runat="server"
                             Text='<%# Bind("RateCharged", "{0:C}") %>'
                             Width="180px">
                    </asp:TextBox>

- Error output:
Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

- How am i certain?:
  if I remove the ($)dollar sign from the textbox field the page can insert or update without errors.

- I tried to remove the dollar sign OnItemInserting and OnItemUpdating but that doesn't seem to work...
  example:
              Protected Sub frmTASK_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs)
                        Dim insRateCharged As TextBox = CType(frmTASK.FindControl("insRateCharged"), TextBox)
                        insRateCharged.Text = Replace(insRateCharged.Text, "$", "")
             End Sub

Can someone help me solve this problem?


0
Comment
Question by:siacom
  • 2
  • 2
5 Comments
 
LVL 25

Accepted Solution

by:
DBAduck - Ben Miller earned 1500 total points
ID: 16410474
You should look at http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.iteminserting(VS.80).aspx and use the Values of the eventArgs.

In VB you have to copy the values to an Array in order to do anything to the values.  I would try the code in the page above and change the value of the entry based on the Key "insRateCharged" and change the value to Replace("$", "")

Ben.
0
 

Author Comment

by:siacom
ID: 16411549
OK I've tried diffenrent ways to change the value without success, from my understanding the OnItemInserting event is handled before inserting..
yet this throws an error:
Protected Sub frmTASK_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs)
        Dim insRateCharged As TextBox = CType(frmTASK.FindControl("insRateCharged"), TextBox)
       
        Dim itemArray(e.Values.Count - 1) As DictionaryEntry
        e.Values.CopyTo(itemArray, 0)
        Dim entry As DictionaryEntry
        For Each entry In itemArray
            If entry.Key.ToString() = "RateCharged" Then
                entry.Value = CDbl(Replace(entry.Value, "$", Nothing))
                insRateCharged.Text = entry.Value  ' I had to add this so the textbox is also changed in case this was the problem.
            End If
       Next
    End Sub

if I include:
  Response.Write(entry.Key.ToString() + ": " + entry.Value.ToString() + "<BR>")
  e.Cancel = True
               ' it will display the correct number without the $ sign.... so the syntax is correct ¿

  insRateCharged(textbox) does not appear as part of the DictionaryEntry...
0
 
LVL 25

Expert Comment

by:DBAduck - Ben Miller
ID: 16413325
OK, so in order to change the value of the inserting value, you will want to go right to the value.

instead of iterating through, which I noticed in VB you must CopyTo and array, so you actually are operating on a copy and not on the live data, so do this instead of iterating through.

e.Values("insRateCharged").Value = e.Values("insRateCharged").ToString().Replace("$", "")

So it would look like this:

Protected Sub frmTASK_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs)
    e.Values("insRateCharged").Value = e.Values("insRateCharged").ToString().Replace("$", "")
End Sub

I am testing some things right now, but I am having to set up the entire thing, so it may take a while.  Try out this code above.

Ben.
0
 

Author Comment

by:siacom
ID: 16414155
*** Thank you for your help, that example worked perfectly. ***

note: the above example has a syntax error for e.Values("insRateCharged") which does not need the .value

... so the sub below works fine ...

Protected Sub frmTASK_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs)
    e.Values("insRateCharged") = e.Values("insRateCharged").ToString().Replace("$", "")
End Sub

if an error occurs with the above, then I tried:
   e.Values("RateCharged") = CDbl(e.Values("RateCharged").ToString().Replace("$", ""))

which forced the textbox to be a double number, accepted in sql for the money field.
0
 

Expert Comment

by:GreatPretender
ID: 20866521
This problem is a real pain, in my opinion.  Why provide users with the ability to use format strings with two-way databinding if it is going to break the databinding in most cases like this (with currencies anyway, and probably dates I'm sure)???

Of course, I don't know what it would have taken for MS to de-format a string once its been formatted, but that sure would have been nice so we don't have to hack it like this.

In any case, I think a better solution for this would be to find a third party control that displays the text properly with a dollar sign, but databind to a property on that control that just returns the value instead of the text.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…
Suggested Courses

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question