Click here to Skip to main content
14,825,178 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HI..

How to multiply two values from textbox and put the result in another textbox in c#...

Here is my code but it getting error, Cannot implicitly convert type 'int' to 'strint'.......

C#
protected void txtqty_TextChanged(object sender, EventArgs e)
   {
       if (txtrate.Text != "")
       {
           txttamt.Text = ((Convert.ToInt32(txtrate.Text.Trim())) * (Convert.ToInt32(txtqty.Text.Trim())));
       }
   }



In SQL i stored the values datatype as integer.......
Posted

Hi,
Pls change as
txttamt.Text = ((Convert.ToInt32(txtrate.Text.Trim())) * (Convert.ToInt32(txtqty.Text.Trim()))).ToString();
   
use below code:-
protected void txtqty_TextChanged(object sender, EventArgs e)
   {
       if (txtrate.Text != "")
       {
           txttamt.Text =Convert.tostring( ((Convert.ToInt32(txtrate.Text.Trim())) * (Convert.ToInt32(txtqty.Text.Trim()))));
       }
   }
   
v2
Comments
[no name] 14-Jul-11 6:31am
   
With your code, he'll get the same error.
Try this one :

C#
protected void txtqty_TextChanged(object sender, EventArgs e)
   {
       if (txtrate.Text != "")
       {
           txttamt.Text = (((Convert.ToInt32(txtrate.Text.Trim())) * (Convert.ToInt32(txtqty.Text.Trim())))).tostring();
       }
   }


What i did here is that i added a bracket at the beginning and another one at the end then converted the whole statement to string.So your error is that you are placing integers in txttamt which accepts strings.
   
Comments
[no name] 14-Jul-11 6:31am
   
I believe you are aware of the fact that C# is a case-sensitive language. It's not tostring(), it is ToString().
You could consider this,
C#
txttamt.Text = (ConvertIntoInt32(txtrate.Text) * ConvertIntoInt32(txtqty.Text)).ToString();

public Int32 ConvertIntoInt32(string dataToConvert)
{
    Int32 result = default(Int32);
    return Int32.TryParse(dataToConvert.Trim(), out result) ? result : result;
}

:)
   
Hi,

Try this

int a= Convert.toInt32(txtrate.Text);
int b=Convert.toInt32(txtqty.Text);

int c=a*b;
txtamt.Text= c;//or Convert.toString(c);



Also you can try without Trim(),it will surely work
   
Comments
rajeshpappachan 25-Jun-12 3:01am
   
Cannot implicitly convert type 'int' to 'string'

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900