2

On Page1.aspx I have 3 txtBoxes with Compare Validators that ensure they are of type Currency.

On Page2.aspx I have another text box called txtResult whose job it is to display the sum of the other 3 txtBoxes when btnCalculate is clicked on Page1.

Problem: I can't get the 3 txtBoxes to be treated as doubles and not strings. If txtBox values are 1, 3, and 8, txtResult will be 138 and not 12.

I tried try parsing but it wasn't allowed failed to compile.

Code behind Page1:

protected void btnCalculate_Click(object sender, EventArgs e)
{
    if (IsValid)
    {
        Server.Transfer("Page2.aspx");
    }
}

Code behind Page2:

if (!IsPostBack)
{
    Page lastPage = (Page)Context.Handler;
    txtResult.Text = ((TextBox)lastPage.FindControl("txtGross")).Text
        + ((TextBox)lastPage.FindControl("txtValueOfAssets")).Text
        + ((TextBox)lastPage.FindControl("txtInvestments")).Text;
}

Attempt to use TryParse:

txtResult.Text = 
    double.TryParse(((TextBox)lastPage.FindControl("txtGross")).‌​Text, out gross);

Errors with:

Cannot implicitly covert bool to string

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
ratboy
  • 63
  • 6
  • Alexei, maybe I did it incorrectly but initially I tried the following: `txtResult.Text = double.TryParse(((TextBox)lastPage.FindControl("txtGross")).Text, out gross);` where gross is a double.... and I got "Cannot implicitly covert bool to string" – ratboy Jan 31 '17 at 21:26
  • For future questions specify exactly what happens ("not allowed" implies there are some outside restrictions and not compile error). I've edited in your comment and close question as duplicate. – Alexei Levenkov Jan 31 '17 at 21:33
  • Thanks for the appropriate edit and suggestion. – ratboy Jan 31 '17 at 22:03

3 Answers3

2

You can use Double.Parse(text) to convert the text into a double. So for example:

txtResult.Text = (Double.Parse(((TextBox)lastPage.FindControl("txtGross")).Text)
    + Double.Parse(((TextBox)lastPage.FindControl("txtValueOfAssets")).Text)
    + Double.Parse(((TextBox)lastPage.FindControl("txtInvestments")).Text)).ToString();

Notice that I needed to wrap the whole thing in a .ToString() to convert the answer back to text so that it can be assigned to txtResult.Text. I provided the answer in on long statement since the questions used one long statement but typically I convert each of the values to a double and place them in individual double vars then I'd add those in another statement, and finally in yet another statement I'd convert the answer to a string and place it in the text box. I find such a multi statement approach easier to read and digest, but that's just personal preference.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
RonC
  • 31,330
  • 19
  • 94
  • 139
  • Ah! I kept `ToString()`ing every line in that statement. I find the multi statement approach easier too. Thanks! – ratboy Jan 31 '17 at 21:11
  • OP specified that "I tried try parsing but it wasn't allowed." - so this is not an answer to question as asked. Otherwise you should have just voted to close as duplicate. – Alexei Levenkov Jan 31 '17 at 21:18
  • @AlexeiLevenkov The question he asked was "How to retrieve values of multiple txtBoxes and display sum in a new page?" and he provided non working code which he wanted help making work. I provided exactly that. Please remove your down vote. – RonC Jan 31 '17 at 21:23
  • OP specified that parsing was *not allowed* - so far I don't see what alternatives are allowed (or even if that statement actually mean what it states), but `Double.Parse` does not look like acceptable option for this question as asked. Clearly they know about `Double.Parse`/`Double.TryParse` based on that line... – Alexei Levenkov Jan 31 '17 at 21:28
  • Turned out OP just used random words... – Alexei Levenkov Jan 31 '17 at 21:29
  • The code I provided uses Double.Parse and does work and it answers the question "How to retrieve values of multiple txtBoxes and display sum in a new page?" when taken together with the code he posted – RonC Jan 31 '17 at 21:29
0

Text property of TextBox is a String type so adding them will result in concatenation. You need to convert the Text value to double before adding.

A simple way of converting is Convert.ToDouble("")

so try this: txtResult.Text = Convert.ToDouble(((TextBox)lastPage.FindControl("txtGross")).Text) + Convert.ToDouble(((TextBox)lastPage.FindControl("txtValueOfAssets")).Text) + Convert.ToDouble(((TextBox)lastPage.FindControl("txtInvestments")).Text);

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Anup Sharma
  • 2,053
  • 20
  • 30
  • OP specified that "I tried try parsing but it wasn't allowed." - so this is not an answer to question as asked. Otherwise you should have just voted to close as duplicate. – Alexei Levenkov Jan 31 '17 at 21:18
0

Since your values are strings, they are being treated as strings, and are concatenating.

You need to convert those values to Double first, and then do your addition.

This post here talks about a few methods for converting to double: Converting string to double in C#

In essence, you can use the Convert.ToDouble() method to convert your text box Text value, to a double.

Convert.ToDouble(((TextBox)lastPage.FindControl("txtValueOfAssets")).Text)

The best approach, would be to look into the TryParse() method, to ensure that your code doesn't break if fed a non-numeric string.

Community
  • 1
  • 1
Eric Burdo
  • 812
  • 1
  • 10
  • 24
  • OP specified that "I tried try parsing but it wasn't allowed." - so this is not an answer to question as asked. Otherwise you should have just voted to close as duplicate. – Alexei Levenkov Jan 31 '17 at 21:18