I am trying to convert a string into integer which contain a mathematical operator e.g "1+1" but I am always getting "Input string was not in a correct format." error during execution.

Recommended Answers

All 21 Replies

What do you expect (hope) to happen?

I am expecting the result should be stored in integer i-e "2"

int a, b, c;

a = Convert.ToInt32(textBox1.Text);
b = Convert.ToInt32(textBox2.Text);
c = a * b;
textBox3.Text = c.ToString();


It will help u.

This is not mine case.
"1+1" is the text of a label/text box and I have to convert the whole expression at once.

What you want to do is not easy.
.Net does not automatically process strings like you expect.
You will need to parse the string yourself; identify the numbers and operators and then perform the appropriate function(s) to get the result.
Google "Parse Math String" as there is a good chance someone else has already done this for you.

alright let me see it.

ok even when I am trying to convert the string (txt1.Text) having value "1" returning me "Input string was not in correct format."

int a = Convert.ToInt32(txt1.Text);

Use debug to examine contents of txt1.Text and confirm that it is just "1".

Ok now I figured out the problem.

I convert the string into int from one button, but in another button i cannot access its current value. its value its always 0 in another button.

Show the code for each button and say where the problem exists.

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Calc
{
    public partial class _Default : System.Web.UI.Page
    {
        int a,b;

protected void btn1_Click(object sender, EventArgs e)
        {
            txt1.Text += Convert.ToString(1);
            
        }
protected void btn2_Click(object sender, EventArgs e)
        {
            txt1.Text += Convert.ToString(2);
            
        }
protected void btnplus_Click(object sender, EventArgs e)
        {
            a = Convert.ToInt32(txt1.Text);
            txt1.Text = a.ToString() + b.ToString();
            // here b is 0, tough updated prior from button19
        } 
protected void Button19_Click(object sender, EventArgs e)
        {

           b = Convert.ToInt32(txt1.Text);
            txt1.Text = a.ToString() + b.ToString();
          // here a is 0, tough updated prior from btnplus
       }
    }
}

I want to access the value of "a" from "btnplus" into "button19" and vise verse.

OK "static" variables solved the problem.

OK. You are using aspx.
When a button is pushed the page is reloaded meaning that the value of a and b are reset.
You can use a HiddenField to persist values across posts to the server, but I am sure there are better ways to do this.
The following is a quote from help for the HiddenField Class.

Normally view state, session state, and cookies are used to maintain the state of a Web Forms page. However, if these methods are disabled or are not available, you can use the HiddenField control to store state values.

I am not an aspx expert (in fact I have no experience with aspx at all).
I recommend you search the forums and/or post a new question to get some help on persisting values across posts to the server.

OK "static" variables solved the problem.

I am not sure that is the answer. This might result in the same value used for every user of your web page.
Check what happens when the page is loaded in two seperate sessions as the same time to confirm what you think.

Whoops!

int a,b,c;

a = Convert.ToInt32(textBox1.Substring(1,1);
b = Convert.ToInt32(textBox1.Substring(2,2);
c = a+b;
textbox3.text = convert.toString(c);

hope this help...

If solved, please mark solved.

just make sure, ur txt1.Text doesnt not have any special characters like '+' that u have mentioned.
for example ur txt1.Text has '1+' or so, u cannot convert it.

please make sure, if ur txt1.Text does not contain any special characters, like +, - etc...
integer type will not consider them, probably that might be one of the reasons, u r not getting it right...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.