Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i have a ptoblem here
Input string was not in a correct format.
and i can't solve it

c# code :-

C#
public decimal totalsalary()
       {
           decimal ms, h, b, m, k, t;

           ms = Convert.ToDecimal(txtmainsalary.Text);
           h = Convert.ToDecimal(txthwaf.Text);
           b = Convert.ToDecimal(txtbdlat.Text);
           m = Convert.ToDecimal(txtmok.Text);
           k = Convert.ToDecimal(txtkhasm.Text);
           t = ms + h + b + m - k;
           return t;


       }



C#
public int totsa(decimal total)
{
    con.Open();
    com.Connection = con;
    com.CommandText = "totsal";
    com.CommandType = CommandType.StoredProcedure;
    com.Parameters.AddWithValue("@totalsalary", total);
    ESalary es = new ESalary();

    int r = com.ExecuteNonQuery();
    con.Close();
    return r;

}



C#
private void button1_Click(object sender, EventArgs e)
{
    empsearchclass esc = new empsearchclass();
    esc.addesalary((int)comboBox1.SelectedValue, Convert.ToDecimal(txtmainsalary.Text), Convert.ToDecimal(txthwaf.Text), Convert.ToDecimal(txtbdlat.Text), Convert.ToDecimal(txtmok.Text), Convert.ToDecimal(txtkhasm.Text));
    totalsalary();
    esc.totsa(Convert.ToDecimal(txttotal.Text));

}



the error here


C#
esc.totsa(Convert.ToDecimal(txttotal.Text));
Posted
Comments
PIEBALDconsult 14-Feb-13 19:54pm    
You might want to use NumericUpDowns instead.
Jibesh 14-Feb-13 20:22pm    
how do you want to process the input the textbox txttotal - ie 10,20,30,40

the value passed to esc.totsa must be 100 is that correct

Instead of Convert (bad word, in this case!), use Decimal.TryParse or Decimal.Parse. It won't improve your code much, but TryParse will help you to avoid exception (but there is nothing wrong with exceptions). More importantly, you will understand better what you are doing. You are parsing, not "converting". And the parse may or may not be successful, depending on input.

As the data comes from UI when a used types invalid text, it is unavoidable in most cases. You need to handle such situations.

—SA
 
Share this answer
 
v2
Comments
Mostafa Metwally 14-Feb-13 19:19pm    
i tried what you say and same problem

my data comes from ui
Sergey Alexandrovich Kryukov 14-Feb-13 19:25pm    
First of all, I'm glad that it's from UI, so you are not doing fundamentally wrong thing. Good.
(Sorry, I could see it's from UI from your code sample, did not pay attention. I'll remove redundant advice from my answer.)

Now, what did you do? Executed under the debugger? What is the input string? the problem is very simple.
Also, do you have exception handling on top of UI cycle. This is done in a special way available in the UI library, so what is that (Forms, WPF, something else?)
—SA
Mostafa Metwally 14-Feb-13 20:03pm    
i'm using Windows Forms and i want to make a salary form to the employee
yes it Executed under the debugger and the input string is textbox
Sergey Alexandrovich Kryukov 14-Feb-13 20:05pm    
What do you mean? Literally "textbox"?! Instead of something like "32332"?
—SA
Jibesh 14-Feb-13 19:55pm    
SA is asking you to check the text you typed in the text box. may be you can reply with the inputs you entered in the textBox.

Also please use the reply link to post your comments so that the author who wrote the solution/comment gets a notification e-mail and he can reply back quickly.
Whatever you do with forms, here is how you should catch and handle all exceptions in the UI thread: How do i make a loop that will stop when a scrollbar reaches the bottom[^].

I may not solve your problem, but this is one of the things you need to do.

—SA
 
Share this answer
 
Comments
Mostafa Metwally 14-Feb-13 20:16pm    
thank you Mr.Sergey
Sergey Alexandrovich Kryukov 14-Feb-13 22:13pm    
You are welcome. Will you accept it formally (green button)? I strongly recommend you to use this exception handling in all cases, even if you do other exception handling in application or even in the same thread but deeper in stack. This is a top level of exception stack in the UI, will catch everything. If you don't to it, you cannot guarantee that your application continues to execute.

(By the way, you never mentioned that you were using comma-separated input, even when you were asked for detail. That's the problem.)
—SA
Espen Harlinn 15-Feb-13 3:45am    
Nice link :-D
Sergey Alexandrovich Kryukov 15-Feb-13 11:13am    
Thank you, Espen.
—SA
If your input is coma separated then you can split the input and process it like below
C#
string []totals = txttotal.Text.Split(',');
foreach(string total in totals)
{
  Decimal decimalTotal;
  Decimal.TryParse(total,out decimalTotal);
  esc.totsa(decimalTotal); 
}


since you havent told how you want to process the txttotal input am just putting the number split code alone. You may adjust this to your requirement.

Update
Use the Decimal.TryParse or Parse as suggested by SA in Solution 1[^] for conversions.
 
Share this answer
 
v3
Comments
Mostafa Metwally 14-Feb-13 20:43pm    
thank you Mr.Jibesh
Jibesh 14-Feb-13 20:50pm    
Glad that helps.
Sergey Alexandrovich Kryukov 14-Feb-13 22:17pm    
My 5... Where did you get this comma, how did you guess..? OP never mentioned that, even when asked for detail.
By the way, I added another answer, to properly catch all UI thread exceptions, an important technique to use in all cases.
—SA
Jibesh 15-Feb-13 4:04am    
OP Reply like this for one your query. you missed this post.

Mostafa Metwally - 7 hrs ago
i'm writing a number in the textbox
like 10 , 20 , 55 ... etc
and then press button
Sergey Alexandrovich Kryukov 15-Feb-13 11:42am    
I see. Yes, I missed it; it wasn't in the original question.
Thank you,
—SA

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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