Click here to Skip to main content
13,598,838 members
Rate this:
Please Sign up or sign in to vote.
i have a ptoblem here
Input string was not in a correct format.
and i can't solve it

c# code :-

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;


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

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


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));


the error here

Posted 14-Feb-13 12:55pm
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
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

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?)
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"?
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.
Mostafa Metwally 14-Feb-13 20:02pm
I'm typing a number in the text box to Calculate the total salary
Sergey Alexandrovich Kryukov 14-Feb-13 20:06pm
You (the user) type the string representing a number. Is it a valid string? What is it, for example?
Mostafa Metwally 14-Feb-13 20:13pm
i'm writing a number in the textbox
like 10 , 20 , 55 ... etc
and then press button
Jibesh 14-Feb-13 20:18pm
OMG - we all thought its a single number you are entering in your text box.

If you want to add a series of number entered into the total text box you need to split the text with coma separated and then calculate the sum.

But its not a good idea to ask the user to enter coma separated value. user may not have any clue how to enter the values unless someone sits near him
Mostafa Metwally 14-Feb-13 20:24pm
yes you are right it's not a good idea to ask the user to enter coma separated value.

ok but now what i have to do
Jibesh 14-Feb-13 20:31pm
see my solution. on how to split the text input for a coma separated text.
Mostafa Metwally 14-Feb-13 20:36pm
thank you Mr.Jibesh
Espen Harlinn 14-Feb-13 19:56pm
You can prevent the user from typing in invalid text using the MaskedTextBox Class
Sergey Alexandrovich Kryukov 14-Feb-13 20:12pm
It may help, but not in all cases. Sometimes this is absolutely useless. It depends on required range. With a small range, NumericUpDown works very well:

I show one more thin in Solution 2, please see.
Espen Harlinn 15-Feb-13 3:44am
True, but when I see a decimal I kind of expect huge values ...
Sergey Alexandrovich Kryukov 15-Feb-13 11:16am
Well, as I said, it may help, not in all cases. It can work in case where all the values which fit in the pattern fit in the range, which is likely in case of decimal. But you cannot express the constraint to the range such as 0 .. 1234561233435...
Espen Harlinn 14-Feb-13 19:56pm
Good points :-D
Sergey Alexandrovich Kryukov 14-Feb-13 20:04pm
Thank you, Espen.
Jibesh 14-Feb-13 20:39pm
+5 for the proper conversion usage
Sergey Alexandrovich Kryukov 14-Feb-13 22:07pm
Thank you.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.

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.)
Espen Harlinn 15-Feb-13 3:45am
Nice link :-D
Sergey Alexandrovich Kryukov 15-Feb-13 11:13am
Thank you, Espen.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

If your input is coma separated then you can split the input and process it like below
string []totals = txttotal.Text.Split(',');
foreach(string total in totals)
  Decimal decimalTotal;
  Decimal.TryParse(total,out 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.

Use the Decimal.TryParse or Parse as suggested by SA in Solution 1[^] for conversions.
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.
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,

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

Advertise | Privacy | Cookies | Terms of Service
Web02-2016 | 2.8.180621.3 | Last Updated 14 Feb 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100