473,394 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

string-int-string generates exception with ConvertTo...

js
Hello,

I have two text boxes accepting ints and displaying their Sum.

This code works:

string ans;
ans = Convert.ToString(Convert.ToInt32(textBoxX.Text) +
Convert.ToInt32(textBoxY.Text));
labelAnswer.Text = ans;

But this code generates InvalidCastException:

labelAnswer.Text = Convert.ToString( Convert.ToInt32(textBoxX.Text) +

Convert.ToInt32(textBoxY) );

Why? What's the difference?

brad.


Nov 15 '05 #1
2 2730
Below.
On Sun, 5 Oct 2003 00:49:45 +0800, "js" <js@nospam.com> wrote:
Hello,

I have two text boxes accepting ints and displaying their Sum.

This code works:

string ans;
ans = Convert.ToString(Convert.ToInt32(textBoxX.Text) +
Convert.ToInt32(textBoxY.Text));
labelAnswer.Text = ans;

But this code generates InvalidCastException:

labelAnswer.Text = Convert.ToString( Convert.ToInt32(textBoxX.Text) +

Convert.ToInt32(textBoxY) );

Assuming this is your actual code, you're asking the Convert class to
convert textBoxY to an Int32. Specify the string, textBoxY.Text, and
you'll be fine.

Austin Ehlers
Nov 15 '05 #2
Below.
On Sun, 5 Oct 2003 00:49:45 +0800, "js" <js@nospam.com> wrote:
Hello,

I have two text boxes accepting ints and displaying their Sum.

This code works:

string ans;
ans = Convert.ToString(Convert.ToInt32(textBoxX.Text) +
Convert.ToInt32(textBoxY.Text));
labelAnswer.Text = ans;

But this code generates InvalidCastException:

labelAnswer.Text = Convert.ToString( Convert.ToInt32(textBoxX.Text) +

Convert.ToInt32(textBoxY) );

Assuming this is your actual code, you're asking the Convert class to
convert textBoxY to an Int32. Specify the string, textBoxY.Text, and
you'll be fine.

Austin Ehlers
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: js | last post by:
Hello, I have two text boxes accepting ints and displaying their Sum. This code works: string ans; ans = Convert.ToString(Convert.ToInt32(textBoxX.Text) + Convert.ToInt32(textBoxY.Text));...
1
by: Chris | last post by:
Hi, I have a DB-table 'AirplaneTypes' that has to 2 fields that form the primary key : 'at_name' and 'at_model' both in the DB defined as type : char 15 MyWinform client : void...
1
by: Peter Wullems | last post by:
I use C# to parse incoming emails for a predefined type and structure of message content and construct replies automatically, attach a file and place the generated emails in the drafts folder. ...
5
by: Nathan Wiegman | last post by:
Using the XmlSerializer to deserialize a double.NaN value generates an exception in the framework which the framework then catches. This is super undesirable in our application, because our...
4
by: Vi | last post by:
Hi, I have an <asp:Button> control on a page. When it is clicked, I run a db stored procedure and then I want to go to the next page by executing a Response.Redirect("NextPage.aspx?id=" + myID)....
0
by: Sagaert Johan | last post by:
I have a tabcontrol whith on one of the tabpages a custom control (mycontrol) derived from UserControl. When i call the tabcontrol.TabPages.Clear() , the mycontrol OnLoad events gets called ....
0
by: =?Utf-8?B?b2xkVkIzcg==?= | last post by:
The following code generates the exception shown below when I call UploadData. I'm trying to call a WCF service using basicHTTP with a manually generated Soap message. Am I on the right track with...
0
by: krystian | last post by:
Hello, I've been following the previous thread "Windows Service Starts and Immediately Stops" and I have a similar problem. I've been trying to get started on a windows service and have...
0
by: fergallydon | last post by:
Hi, I'm trying to implement some centralised exception handling and logging in the Application_Error() event of global.asax. If the code that generates the exception is not inside a try-catch...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.