Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 4th, 2004, 12:19 PM
Authorized User
 
Join Date: Jun 2004
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Default Cast from string "4.5" to type 'Decimal' is not va


Hi experts,
I am having problem in "converting String to Decimal."
I worte a simple function that gets values from text boxes.
It makes a calculation and returns a value.

The code is written below.
The funny thing is whent I enter values 1, xxxx, 4 , it returned 96, but when I tried 1, xxxx, 4.5 I have got "Cast from string "4.5" to type 'Decimal' is not valid." error.

How do we convert String to Decimal? please help me

Exception Details: System.InvalidCastException: Cast from string "4.5" to type 'Decimal' is not valid.

The code is:

<%@ Page Language="VB" %>
<%@ Import NameSpace="System.Data" %>

<script runat="server">

sub Submit(Sender as Object , e as EventArgs)

    Calculate(tbUserID.Text,tbPayee.Text, tbAmount.Text)

End sub

public function Calculate(UseId as Integer, payee as String, decAmount as Decimal) As String


        dim tempdecAmount as decimal
             tempdecAmount = Convert.ToDecimal(decAmount)
         response.write(useID)
         response.write(payee)
         dim Balance as Decimal
         if tempdecAmount <= 100 then
            Balance = 100 - tempdecAmount
            response.write(Balance)
         end if

         Return Balance
end function

</script>

<html><body>
      <form runat="server">

             UserID:
             <asp:TextBox id="tbUserID" runat="server"/>
             Payee:
             <asp:TextBox id="tbPayee" runat="server"/>
             Amount:
             <asp:TextBox id="tbAmount" runat="server"/>
             <asp:Button id="btSubmit" runat="server" text="Submit" OnClick="Submit"/><p>

        </form>
</body></html>



 
Old July 4th, 2004, 04:18 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 336
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alyeng2000
Default

is there a problem if you pass parameters like that
Calculate(Cint(tbUserID.Text),tbPayee.Text, CDec(tbAmount.Text))


Ahmed Ali
Software Developer
 
Old July 4th, 2004, 09:09 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

The problem is that you have written a function that is expecting a decimal, but you are passing a string. So you need to either write the function to accept a string and do the conversion internally, or pass the function a decimal (which is really the proper way to do it). Then you'll need to do the conversion first as Ahmed suggested.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Cast from string 'OPEN' to type 'Double' is not va yasinirshad .NET Framework 1.x 1 August 12th, 2007 12:45 PM
Cast from string "" to type 'Double' is not dounme ADO.NET 1 March 8th, 2005 01:31 PM
Cast from string "" to type 'Double' is not dounme SQL Server 2000 1 February 18th, 2005 01:44 AM
Cast from string "" to type 'Integer' is not valid h3x SQL Server ASP 1 January 30th, 2005 09:35 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.