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

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?


siacomAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DBAduck - Ben MillerPrincipal ConsultantCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
siacomAuthor Commented:
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
DBAduck - Ben MillerPrincipal ConsultantCommented:
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
siacomAuthor Commented:
*** 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
GreatPretenderCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.