I'm just wondering where i could find out information on how to code calculations such as addition, subtraction, multiplication and division in asp.net/C# ? I may need to do sums with database values aswel so any info on these would also be appreciated!

int Total = lblGC.Text;
int Quantity = txtPurchNS.Text;
int Price = txtPP.Text;
Total = Quantity * Price;

when I run the application, I get the error "Cannot implicity convert type 'String' to 'int'". How do I get rid of this?

Also, I hope to later store the values of the label and two textboxes to my databases where they will be going to varchar(50) columns. If I convert them to int now, will it cause errors later on?

You are getting this error because the contents of the textbox wasn't an integer. If you type a numeric character like 9 in a textbox the int.Parse method will know what to do with it. If you type in a characters like test or just leave it blank the int.Parse
will have no idea how to convert it.

## timmyc21

Member

4 Points

33 Posts

## code for addition/subtraction/multiplication/division in asp.net and C#

Feb 10, 2008 02:54 PM|timmyc21|LINK

Hi,

I'm just wondering where i could find out information on how to code calculations such as addition, subtraction, multiplication and division in asp.net/C# ? I may need to do sums with database values aswel so any info on these would also be appreciated!

Regards,

Timmyc21

## jamesqua

Star

8319 Points

1432 Posts

## Re: code for addition/subtraction/multiplication/division in asp.net and C#

Feb 10, 2008 03:49 PM|jamesqua|LINK

You can use traditional arithmitic operators like + - * / with integers and other numeric variables.

example

int total;

int operand1 = 5;

int opeand2 = 6;

total = operand1 + operand2

total will equal 11.

## timmyc21

Member

4 Points

33 Posts

## Re: code for addition/subtraction/multiplication/division in asp.net and C#

Feb 21, 2008 04:28 PM|timmyc21|LINK

I have the following code................

int Total = lblGC.Text;

int Quantity = txtPurchNS.Text;

int Price = txtPP.Text;

Total = Quantity * Price;

when I run the application, I get the error "Cannot implicity convert type 'String' to 'int'". How do I get rid of this?

Also, I hope to later store the values of the label and two textboxes to my databases where they will be going to varchar(50) columns. If I convert them to int now, will it cause errors later on?

## jamesqua

Star

8319 Points

1432 Posts

## Re: code for addition/subtraction/multiplication/division in asp.net and C#

Feb 21, 2008 05:09 PM|jamesqua|LINK

You need to explicitly convert the string objects to type int

int Total = int.Parse(lblGC.Text);

int Quantity = int.Parse(txtPurchNS.Text);

int Price = int.Parse(txtPP.Text);

Total = Quantity * Price;

When you pass the int objects to the database just make sure you call .ToString() method on the object when you pass it

## timmyc21

Member

4 Points

33 Posts

## Re: code for addition/subtraction/multiplication/division in asp.net and C#

Feb 21, 2008 08:53 PM|timmyc21|LINK

Thanks for the advice. I have changed my code and it now looks like this.......................

Decimal Total = Decimal.Parse(lblGC.Text);

int Quantity = int.Parse(txtPurchNS.Text);

Decimal Price = Decimal.Parse(txtPP.Text);

Total = Quantity * Price;

The only problem is im now getting an error "

Input string was not in a correct format."Any idea as to what I may have to change?

## jamesqua

Star

8319 Points

1432 Posts

## Re: code for addition/subtraction/multiplication/division in asp.net and C#

Feb 21, 2008 09:03 PM|jamesqua|LINK

You are getting this error because the contents of the textbox wasn't an integer. If you type a numeric character like 9 in a textbox the int.Parse method will know what to do with it. If you type in a characters like test or just leave it blank the int.Parse will have no idea how to convert it.

## timmyc21

Member

4 Points

33 Posts

## Re: code for addition/subtraction/multiplication/division in asp.net and C#

Feb 21, 2008 09:24 PM|timmyc21|LINK

but im putting integer/decimal values in the appropriate textboxes and the error is still generated. Any idea as to why so?

## jamesqua

Star

8319 Points

1432 Posts

## Re: code for addition/subtraction/multiplication/division in asp.net and C#

Feb 22, 2008 01:31 AM|jamesqua|LINK

What values are you putting in for the decimal values?