Click here to Skip to main content
15,907,392 members
Please Sign up or sign in to vote.
3.40/5 (3 votes)
See more:
Dear Friends

i have multiple text boxes and i am trying to calculate in another box but i failed to run if any one text box value equal to null then string error message comes
C#
private void button2_Click(object sender, EventArgs e)
       {
           double a1 = double.Parse(txtRawMm.Text);
           double b1 = double.Parse(txtCsp.Text);
           double c1 = double.Parse(txtPf.Text);
           double d1 = double.Parse(txtSe.Text);
           double e1 = double.Parse(txtIowc.Text);
           //double f1 = double.Parse(txtTotal.Text);
           double i = a1 + b1 + c1 + d1 + e1;
           i = a1 + b1 + c1 + d1 + e1;
           //txtTotal.Text = "Sum is: " + sum;
           //Double f1 = NetIncome / Sales * 100;
           txtTotal.Text = i.ToString("C").Remove(0, 1);
Posted

Implement some basic checks on your data.

I would check for the following:

1) Are strings null/empty/whitespace (Try using String.IsNullOrWhiteSpace())
2) Are the strings numerical values?

Or if you don't want multiple checks like that just use Double.TryParse instead and look at the result before you try and sum them. That method returns a boolean to indicate if the parse worked or not. It also takes a second parameter which is the output of the parse should it be successful.
 
Share this answer
 
v2
HI
Try this hope this will solve your problem

JavaScript
<script type="text/javascript">
        function sum() {
            var txtFirstNumberValue = document.getElementById('TextBox1').value;
            var txtSecondNumberValue = document.getElementById('TextBox2').value;
            var txtThridNumberValue = document.getElementById('TextBox3').value;
            var txtFourthNumberValue = document.getElementById('TextBox4').value;

            if (txtFirstNumberValue == "")
                txtFirstNumberValue = 0;

            if (txtSecondNumberValue == "")
                txtSecondNumberValue = 0;

            if (txtThridNumberValue == "")
                txtThridNumberValue = 0;

            if (txtFourthNumberValue == "")
                txtFourthNumberValue = 0;

            var result = parseInt(txtFirstNumberValue) + parseInt(txtSecondNumberValue) + parseInt(txtThridNumberValue) + parseInt(txtFourthNumberValue);
            if (!isNaN(result)) {
                document.getElementById('TextBox5').value = result;
            }
        }
    </script>


XML
<asp:TextBox ID="TextBox1" runat="server" onkeyup="sum();"></asp:TextBox>
      <asp:TextBox ID="TextBox2" runat="server" onkeyup="sum();"></asp:TextBox>
      <asp:TextBox ID="TextBox3" runat="server" onkeyup="sum();"></asp:TextBox>
      <asp:TextBox ID="TextBox4" runat="server" onkeyup="sum();"></asp:TextBox>
      <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>



check if it works for you
 
Share this answer
 
v2
Use Double.TryParse[^] method (or properly catch the Parse method exception).
By the way. You should use arrays (for instance an array of textboxes and another one of doubles), your code would be cleaner.
 
Share this answer
 
USE The double.TryParse method converts strings into double. It never throws an exception—even on invalid input and null. It is called in a different way than double.Parse. It is overall preferable to double.Parse in most program contexts.

C#
double a1, b1, c1, d1, e1, i;
double.TryParse(txtRawMm.Text,out a1);
double.TryParse(txtCsp.Text,out b1);
double.TryParse(txtPf.Text,out c1);
double.TryParse(txtSe.Text,out d1);
double.TryParse(txtIowc.Text,out e1);
//double f1 = double.Parse(txtTotal.Text);
i = a1 + b1 + c1 + d1 + e1;
//txtTotal.Text = "Sum is: " + sum;
//Double f1 = NetIncome / Sales * 100;
if(i > 0)
txtTotal.Text = i.ToString("C").Remove(0, 1);
 
Share this answer
 
v2
Hi
Whenever you are getting a value from Textbox Please note these thing,

Trim the value of Text box, because the user might have entered space by mistake, space is also a character which will result in wrong data.
So always Trim the values ..

If you want to cast a data from text box to Int or Double , instead of Convert.ToDouble or Double.Parse , you can use TryParse for Safe conversion.

Below is my code... If it is useful you can use this..

C#
double a1, b1, c1, d1, e1;
                double.TryParse(txtRawMm.Text.Trim(), out a1);
                double.TryParse(txtCsp.Text.Trim(), out b1);
                double.TryParse(txtPf.Text.Trim(), out c1);
                double.TryParse(txtSe.Text.Trim(), out d1);
                double.TryParse(txtIowc.Text.Trim(), out e1);
 
Share this answer
 
Hi here you can use Conditional operator like

C#
double a1 = double.Parse(!string.IsNullOrEmpty(txtRawMm.Text)?txtRawMm.Text:0);


If you are using Asp.net then keep Required field validator & Regular Expression Validator to avoid null or Sring values.
 
Share this answer
 
Always check for null and white space using String.IsNullOrWhiteSpace Method[^].
C#
if(!String.IsNullOrEmpty(txtRawMm.Text))
{
    // Read the value.
}
 
Share this answer
 
Use This function

C#
public static double GetNumeric(object objValue)
        {
            double dblNVal = 0;
            if (Convert.ToString(objValue) == "" || objValue == null)
                return 0;
            else
                try
                {
                    dblNVal = Convert.ToDouble(objValue);
                }
                catch { dblNVal = 0; }
            return dblNVal;
        }
 
Share this answer
 

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