Hi,
i am trying to accumulate a total to be stored in my 'targetTotal' variable of type int which is then to be sent to my msaccess db into a field called 'targetTotal' of type LongInteger, the trouble i am having now is that after adding my totals given the following code snippet:

int targetOneDay =0;
            int targetTwoDay = 0;
            int targetFiveDay = 0;
            int targetTenDay = 0;
            int targetTotal =0;
            int total = 0;
            targetTotal = targetOneDay + targetTwoDay + targetFiveDay + targetTenDay;

i get an error which says the following: 'Input string was not in correct format' pointing to the target total variable. So i looked online for this particular error and based on my understanding i have to check if it is a null value and convert it accordingly. So what i first did was created another variable called 'total' of type int, removed the 'text' property of my target total and added the following if statement:

targetTotal = Convert.ToInt32(totaltargettxtbx);

            if (targetTotal != null)

                total = Convert.ToInt32(targetTotal);
            else
                total = 0;

now when i run my applicantion i get another error saying: Unable to cast object of type 'System.Windows.Forms.TextBox' to type 'System.IConvertible'. which is pointing to my targetTotal variable.
Can somebody kindly tell me why i'm getting these errors and how exactly i can resolve please.

Recommended Answers

All 5 Replies

hmmm..you havent explained very well..can you post more of your code?

If I am right that totaltargettxtbx is referring to a TextBox instance, then you cannot convert it to an integer. Use totaltargettxtbx.Text instead.

If I am right that totaltargettxtbx is referring to a TextBox instance, then you cannot convert it to an integer. Use totaltargettxtbx.Text instead.

Ddandbe, i changed it to totaltargettxtbx.Text with a slight updated IF statement:

targetTotal = Convert.ToInt32(totaltargettxtbx.Text); // error pointing here
            
            targetTotal += targetOneDay + targetTwoDay + targetFiveDay + targetTenDay;
            
            if (totaltargettxtbx.Text != null)

                total = Convert.ToInt32(targetTotal);
            else
                total = 0;

i get the same error message: Input string was not in a correct format, and it is a textbox. James6754, my goal in this instance is to store the total of all the target days in targetTotal and send that into an ms access database into a field called 'targetTotal' of type long integer when i hit my submit button. My problem is that it does not accumulate total, when i hover mouse over the variables e.g targetOneDay i can see the values that i have entered for their corresponding textboxes but when i hover my mouse over targetTotal varibale it displays 0, hope that clears things up. The code is all there, that is the code in question but here is a slighly expanded code snippet, but again it is the same as above so no need to show anything new.

private void submitbtn_Click(object sender, EventArgs e)
        {
            string projectName;
            string startDate;
            string endDate;

            int targetOneDay =0;
            int targetTwoDay = 0;
            int targetFiveDay = 0;
            int targetTenDay = 0;
            int targetTotal =0;
            int total = 0;



            targetOneDay = Convert.ToInt32(nine_to_fourteenhrstxbx.Text); 
            targetTwoDay = Convert.ToInt32(fifteen_to_fortfourglhtxtbx.Text);
            targetFiveDay = Convert.ToInt32(fourtyfive_to_74txtbx.Text);
            targetTenDay = Convert.ToInt32(min75glhtxtbx.Text);
            targetTotal = Convert.ToInt32(totaltargettxtbx.Text);
            
            targetTotal += targetOneDay + targetTwoDay + targetFiveDay + targetTenDay;
            
            if (totaltargettxtbx.Text != null)

                total = Convert.ToInt32(targetTotal);
            else
                total = 0;
                
                // sql statement missed


}

There may be leading or trailing whitespace in the textboxes, that would mean that the string passed to the ToInt32 method could be "3546 "; this would cause an error.

try using Convert.ToInt32(textbox.Text.[B]Trim()[/B]); this will make sure there is no whitespace, also ddanbe has a pretty good code snippet in the code snippet library of a textbox control that only accepts digits and decimals, so it would guarantee no parse errors.

What test data are you inputting?

If you are entering very large values then it is bound to fail as an integer may not be able to hold that much.

Try entering small test data, e.g. 1,2,3 and see the result, if it works then you may just have to change the type of targetTotal to long

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.