Hello all,
I am having a problem performing a calculation while in formview. I am using visual studio 2005, sql and C#. I have many textbox's that a user inputs, some of these require the calculation of subtotal and total. Here is a small example of what I have tried;

protected void Button1_Click1(object sender, EventArgs e)
{
//declare variables
decimal subtotal10:
decimal hour1010;
decimal hour1011;
decimal hour1020;

//get information from form
hour1010 = ((TextBox)FormView1.FindControl("Hours1010TextBox"));
hour1011 = ((TextBox)FormView1.FindControl("Hours1011TextBox"));
hour1020 = ((TextBox)FormView1.FindControl("Hours1020TextBox"));

//calculate hours
subtotal10 = hour1010 + hour1011 + hour1020 *1;

//display hours
TextBoa a = (TextBox)FormView1.FindControl("SubHours10TextBox");
a.Text = subtotal10;
FormView1.FindControl("TotalHoursTextBox").Focus();
}

All that I have tried decimal.parse, .Text, .ToString and I am getting error messages Can not cast string to int, int to string, decimal to int and all that fun stuff. The closest I have some in a concat in the subhours textBox.

What am I missing?? Any ideas??
Thanks

I am new so forgive me if I am making a stupid statment here...

I have no idea what language you are posting there, but in C# I would have written the same thing like this...

/*
      * You can declare and set the fields here
      * You also need to parse the string input
      * I don't know about using the decimal type I am used to using double
      */
     double subtotal10;
     double hour1010 = double.Parse(Hours1010TextBox.Text);
     double hour1011 = double.Parse(Hours1011TextBox.Text);
     double hour1020 = double.Parse(Hours1020TextBox.Text);

     // calculate hours
     // I have no idea why you multiply by 1, doing so results in the
     // same value... i.e. - 1 * 1 = 1
     // So I took it out
     subtotal10 = hour1010 + hour1011 + hour1020;

     // display hours
     // Now you need to take the double value and return it as a string
     // Use the ToString() method for that
     SubHours10TextBox.Text = subtotal10.ToString();

Hope that helps :cheesy:

Thanks I will try this. As to the * 1, because it is coming out of a textbox, this insures that it is a neumeric value. I will let you know how it goes. Your way looks like it will save me a lot.

Thanks I will try this. As to the * 1, because it is coming out of a textbox, this insures that it is a neumeric value. I will let you know how it goes. Your way looks like it will save me a lot.

Ah I see.

Well then in that case you should use the try/catch statment to trap an error if one is present.

I modified the code to trap the error. Try this then...

public partial class Form1 : Form
    {
        /*
         * Fields
         * Here you set the private fields so that they cannot
         * be directly manipulated
         */
        private double _hour1010;
        private double _hour1011;
        private double _hour1020;
        private double _subTotal;

        /*
         * Properties
         * You set these to be manipulated by your program
         */
        public double Hour1010
        {
            get { return _hour1010; }
            set { _hour1010 = value; }
        }
        public double Hour1011
        {
            get { return _hour1011; }
            set { _hour1011 = value; }
        }
        public double Hour1020
        {
            get { return _hour1020; }
            set { _hour1020 = value; }
        }
        public double SubTotal
        {
            get { return _subTotal; }
            set { _subTotal = value; }
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                // Here we use the double.Parse() to convert
                // the textBox strings to numbers so we can use them
                Hour1010 = double.Parse(textBoxHour1010.Text);
                Hour1011 = double.Parse(textBoxHour1011.Text);
                Hour1020 = double.Parse(textBoxHour1020.Text);
            }
            catch
            {
                // Throw an error here if something didn't parse
                MessageBox.Show("There was an error!");
            }

            // Do your calculation
            SubTotal = Hour1010 + Hour1011 + Hour1020;

            // Output the results
            // Use ToString() to convert the double to a string
            textBoxHoursSubTotal.Text = SubTotal.ToString();
        }
    }

There are some other minor things I modified to make the code a little more proper (I think! lol)

Hope that helps! :cheesy:


Edit:: Also I named my textboxes the way I would name them. (i.e. textBoxHoursSubTotal instead of SubHours10TextBox)

Both work well on regular web pages or on html type pages but in Visual Studio 2005 FormView using the FormView.FindControl throws an error of invalid arguement with double.Parse. FormView is databound and I believe you must use the FindControl to get or set the data in the TextBox. I am not so sure anymore. My brain has gone to mush.

Found it. You were close I went back created a new site with a short form and a sql db. Using your start, this is what I came up with;
protected void Button1_Click(object sender, EventArgs e)
{
string num1 = ((TextBox)FormView1.FindControl("num1TextBox")).Text;
string num2 = ((TextBox)FormView1.FindControl("num2TextBox")).Text;
double total;
total = double.Parse(num1) + double.Parse(num2);
((TextBox)FormView1.FindControl("totalTextBox")).Text = total.ToString();
//This gave me a concatination
//((TextBox)FormView1.FindControl("totalTextBox")).Text = ((TextBox)FormView1.FindControl("num1TextBox")).Text + ((TextBox)FormView1.FindControl("num2TextBox")).Text;
}

Once I got back to the concat I tried a few variations but was getting convert string to double errors again. I left the variables as strings and then parsed them.

Thanks for your assistance with this.

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.