473,394 Members | 1,875 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 2729
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: 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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.