I am performing a calculation inside a FormView Insert Template using Visual Studio 2005 and C#. The calculation works fint until a TextBox is left empty. Then I get an expected error message;

Input string was not in a correct format.

There are 100 TextBoxes on this form at which any 80% can be left blank. This is acceptable.

The TextBoxes are databound and if I change the text in the properties, I lose the databind.

This is where I need help;
I need to populate the TextBoxes with a value if the user has not so the calculation can continue.

Here is my code so far;

protected void Button1_Click(object sender, EventArgs e)
    {
        string num1 = ((TextBox)FormView1.FindControl("num1TextBox")).Text;
        string num2 = ((TextBox)FormView1.FindControl("num2TextBox")).Text;
         string num3 = ((TextBox)FormView1.FindControl("num3TextBox")).Text;
        double total;
        total = double.Parse(num1) + double.Parse(num2) + double.Parse(num3);
        ((TextBox)FormView1.FindControl("totalTextBox")).Text = total.ToString();

    }

Again if all boxes are filled it works fine.

Thanks,
gene

Recommended Answers

All 2 Replies

if the texbox was bound to the database and there is no value then num1 num2 etc. is empty or null, neither of which will parse to a double to be able to be added. You need to do a test to see if they are null or empty and make thier value 0.
The easiest way is to set the value to 0 when you pull it from the database (either set the value to 0 or put 0 in its place in the stored proc). Or you can do it just before you do your sum.

The text boxes are null from the database, I programmed a solution, while a little backwards, with so many textboxes to fill out it seemed silly to have the client do it.

I simply created a populate/clear button and with the click of a single button all the text boxes were filled with a "0". The client can fill out what textboxes are necessary and then calculate.

protected void Button2_Click(object sender, EventArgs e)
    {
        //Clear Hours and prepopulate with "0"
        ((TextBox)FormView1.FindControl("num1TextBox")).Text = "0";
        ((TextBox)FormView1.FindControl("num2TextBox")).Text = "0";
        ((TextBox)FormView1.FindControl("num3TextBox")).Text = "0";

}

Hope this helps.

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.