Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
aoa... i am updating my record on the basis of employee name... but when i am adding two numeric values like (200.00+100.00 ) ans is giving zero...but when i am giving simple values like (100+200) then it is giving correct answers.... what can i do for this?????? that ans of numeric values will also b correct
C#
int val1, val2;
            int.TryParse(textBox3.Text, out val1);
            int.TryParse(textBox4.Text, out val2);
            int sum = val1 + val2;
            textBox5.Text = sum.ToString();






            if (textBox1.Text != "" && textBox2.Text != "")
            {
                SqlCommand sqlcmd = sqlconn.CreateCommand();
                sqlcmd.CommandText = " update payroll set  Gross_Salary='" + val1 + "',Bonus='" + val2 + "',total_salary='" + val3 + "',income_tax='" + val6 + "',eobi='" + val5 + "',advance='" + val4 + "',fine='" + val8 + "',others='" + val9 + "',net_salary='" + val7 + "' where emp_name='" + textBox1.Text + "' ";
                try
                {
                    sqlcmd.ExecuteNonQuery();
                    MessageBox.Show("REcord updated");
                }
                catch (SqlException err)
                {
                    MessageBox.Show(err.Message);
                }
            }
            else
            {
                MessageBox.Show("Enter record to update");
            }
Posted
Updated 17-Aug-14 21:12pm
v2
Comments
[no name] 18-Aug-14 3:14am    
What is your data types of given values
Member 10690757 18-Aug-14 3:17am    
numeric
Sergey Alexandrovich Kryukov 18-Aug-14 3:20am    
Did you hear the question: "What is your data types of given values?". "numeric" is not an answer.
—SA

The lines 3-4 of your code has a bug which makes reading the rest of the code useless. The methods TryParse has Boolean result type; if they return false, the out parameters does no get proper results, because parsing was unsuccessful. You are throwing out this result, this way, ignoring the case of unsuccessful parsing.

There are other problems.

Don't use "", use string.Empty. And there is a really critical problem: you compose a query using concatenation of strings taken from UI. Repeating concatenation is bad, because strings are immutable; but, much worse, this opens wide the possibility for the well-known exploit called SQL injection:
http://xkcd.com/327[^].

Never ever do it. Please see: http://en.wikipedia.org/wiki/SQL_injection[^].

Use this: http://msdn.microsoft.com/en-us/library/ff648339.aspx[^].

For explanations, please see my past answers:
EROR IN UPATE in com.ExecuteNonQuery();[^],
hi name is not displaying in name?[^].

—SA
 
Share this answer
 
Use Double.Parse instead of Int
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 18-Aug-14 3:19am    
That's correct (voted 4), but there are other, worse problems, even in these TryParse lines. Please see my answer.
—SA
Hemant L Patil 18-Aug-14 3:45am    
ok textBox5.Text = convert.tostring(sum);

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