Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody!
I have asp.net project I want to calculate numbers by 9 label.text and 9 textbox.text values but when I input two of them I have problem but when I fill all of them there is no problem.:my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class dieh : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            dbmanager dbm = new dbmanager();
            dbm.fillddl(DropDownList1, "dieh", "sal_dieh", "derham");
            dbm.fillddl(DropDownList2, "dieh", "sal_dieh", "derham");
            dbm.fillddl(DropDownList3, "dieh", "sal_dieh", "derham");
            dbm.fillddl(DropDownList4, "dieh", "sal_dieh", "derham");
            dbm.fillddl(DropDownList5, "dieh", "sal_dieh", "derham");
            dbm.fillddl(DropDownList6, "dieh", "sal_dieh", "derham");
            dbm.fillddl(DropDownList7, "dieh", "sal_dieh", "derham");
            dbm.fillddl(DropDownList8, "dieh", "sal_dieh", "derham");
            dbm.fillddl(DropDownList9, "dieh", "sal_dieh", "derham");
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label1.Text = DropDownList1.SelectedValue.ToString();
 
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label2.Text = DropDownList2.SelectedValue.ToString();
    }
    protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label3.Text = DropDownList3.SelectedValue.ToString();
    }
    protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label4.Text = DropDownList4.SelectedValue.ToString();
    }
    protected void DropDownList5_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label5.Text = DropDownList5.SelectedValue.ToString();
    }
    protected void DropDownList6_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label6.Text = DropDownList6.SelectedValue.ToString();
    }
    protected void DropDownList7_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label7.Text = DropDownList7.SelectedValue.ToString();
    }
    protected void DropDownList8_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label8.Text = DropDownList8.SelectedValue.ToString();
    }
      protected void DropDownList9_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label9.Text = DropDownList9.SelectedValue.ToString();
    }
      protected void Button2_Click(object sender, EventArgs e)
      {
 
          Decimal lbl1; 
          Decimal.TryParse(Label1.Text.Trim(), out lbl1);
          Decimal lbl2;
          Decimal.TryParse(Label2.Text.Trim(), out lbl2);
          Decimal lbl3;
          Decimal.TryParse(Label3.Text.Trim(), out lbl3);
          Decimal lbl4;
          Decimal.TryParse(Label4.Text.Trim(), out lbl4);
          Decimal lbl5;
          Decimal.TryParse(Label5.Text.Trim(), out lbl5);
          Decimal lbl6;
          Decimal.TryParse(Label6.Text.Trim(), out lbl6);
          Decimal lbl7;
          Decimal.TryParse(Label7.Text.Trim(), out lbl7);
          Decimal lbl8;
          Decimal.TryParse(Label8.Text.Trim(), out lbl8);
          Decimal lbl9;
          Decimal.TryParse(Label9.Text.Trim(), out lbl9);
          Decimal txt1 = Decimal .Parse(TextBox1.Text);
          Decimal txt2 = Decimal .Parse(TextBox2.Text);
          Decimal txt3 = Decimal .Parse(TextBox3.Text);
          Decimal txt4 = Decimal .Parse(TextBox4.Text);
          Decimal txt5 = Decimal .Parse(TextBox5.Text);
          Decimal txt6 = Decimal .Parse(TextBox6.Text);
          Decimal txt7 = Decimal .Parse(TextBox7.Text);
          Decimal txt8 = Decimal .Parse(TextBox8.Text);
          Decimal txt9 = Decimal .Parse(TextBox9.Text);
          Decimal total;
          Decimal der;
          Decimal result;
          total = (txt1 / lbl1) + (txt2 / lbl2) + (txt3 / lbl3) + (txt4 / lbl4) +(txt5 / lbl5) + (txt6 / lbl6) + (txt7 / lbl7) + (txt8 / lbl8);
          der = total * lbl9;
          result = txt9 - der;
          Label10.Text = result.ToString();
      }
    
}
Posted
Updated 16-Oct-15 2:19am
v3
Comments
PIEBALDconsult 15-Oct-15 15:44pm    
0) NEVER use Convert.
1) Try to use NumericUpDowns when you want nummbers; not TextBoxes.
2) Don't enable Button2 unless all the values are filled in.
3) Possibly store the numeric values of the labels in the label tags.
4) _Test_ for null or use the null-coalescing operator (??).
Philippe Mori 15-Oct-15 18:46pm    
5) Give meaningfull names to controls.
6) Add a function that take a textbox or label and returns the value. If you have problem like this one, you only have to modify code once.
7) Event handler should be private.
8) Variable names for controls should start with a lowercase.
9) Declare variable at their first use.
10) Avoid hardcoded string in a function. It should probably be a member of the class and same formatting would probably be used for other fields.
rezaeti 15-Oct-15 15:49pm    
what changes do i do in my code?the value of labels are values of database that load by dropdownlist selectedvalues

First, you don't check to see if any of those textboxes or the labels are blank. You cannot convert an empty string to a decimal value.

Second, you do no validation of the data in these boxes at all so you have no idea if these are valid numbers or not. What if the labels have 0's in them? Ever hear that you can't divide by 0?

Next why are you parsing the text in a label of all things to do calculations!?!?
 
Share this answer
 
v2
Comments
rezaeti 15-Oct-15 15:53pm    
the value of labels are not equal zero .the values of them loads from db by dropdownlist.selectedvalues.
please say what do i do?
Dave Kreskowiak 15-Oct-15 16:26pm    
I already told you why your code doesn't work when only 2 numbers are entered in the textboxes.
Obviously, your own code is like a blackbox to you. It don't do what you expectand you don't understand why.
What follow is not directly a solution to your problem, but a key that will help you to understand by yourself what is wrong.
The debugger is your friend. It will show you what your code is really doing.
Follow the execution, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

And beware of divisions by zero.
 
Share this answer
 
v2
First of all you see comments in your question and Solution 2.

You really need to give a meaningful name to your controls. Its really embarrassing to correct it with same name.
Anyways, instead of using Convert.ToDecimal you can use Decimal.TryParse[^]:

C#
protected void Button2_Click(object sender, EventArgs e)
{
    Decimal lbl1;
    Decimal.TryParse(Label1.Text.Trim(), out lbl1);
    //Your code goes here
}


--Amy
 
Share this answer
 
Comments
rezaeti 16-Oct-15 8:18am    
hi I edit my code as above but yet does not work:
rezaeti 16-Oct-15 8:42am    
when I set initial value of lbl's = 1 the error solved but I want to handled basically

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900