Bellow is the code i am using to connect to a webservice that i created that has basic math web methods like add, subtract, etc... this is the button for my windowsform app that connects to the webservice it has 2 text boxes a label to display the answer and a button thus the code below. I am getting the dreaded cannot convert from 'string' to 'int' I tried using convert or int.parse but i am having trouble how i would properly do this.

private void addbutton_Click(object sender, System.EventArgs e)
		{
			iis.useractive.rsummey.Service1 theWebService = new iis.useractive.rsummey.Service1();
	       int results = theWebService.Add(textBoxAddNum1.Text, textBoxAddNum2.Text); 

			labelAddAnswer.Text =  results.ToString(); 


		   
		}

Thank you !

I got it here is how i did it

int x = Int32.Parse(textBoxAddNum1.Text);
			int y = Int32.Parse(textBoxAddNum2.Text); 


			iis.useractive.rsummey.Service1 theWebService = new iis.useractive.rsummey.Service1();
	        int results = theWebService.Add(x,y);
			labelAddAnswer.Text =  results.ToString();

Are you sre those two textBoxes really contains integers ONLY?
Usually you get this kind of error, if the string, which you want to convert to integer, does NOT contains only numbers (with not letters, no dots, or commas).

Do a check if there really isa int:

private void method()
        { 
            int x=0;
            int y=0;
            int result=0;
            string s1 = textBox1.Text.Trim();
            string s2 = textBox2.Text.Trim();
            bool bChecking = int.TryParse(s1, out x);
            if (bChecking)
            {
                bChecking = int.TryParse(s2, out y);
                if (bChecking)
                { 
                //both stirngs are converted to integer successfully!
                    //do the code here!
                }
            }
            if (!bChecking)
                MessageBox.Show("Input are not integers.");
        }

Hope it helps,
Mitja

In my web service i have a method called Add and in that I just have it taking in two int x,y and adding them together. Back in my windows form i have two text boxes you see in my code above. Every thing works now. Are you saying i should use tryparse instead of int32.parse?

No, but this is only to check if the value in textBox is really an integer. If its not, the method will not continue, but will throw an error message.

About your issue, better use Convert method:

int x = Convert.ToInt32(textBoxAddNum1);
//for the y do the same.

Thank you for your help and advice ! :)

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.