Click here to Skip to main content
Rate this: bad
good
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) 
        {
            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;
 
        }
 

        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

 
 esc.totsa(Convert.ToDecimal(txttotal.Text));
Posted 14-Feb-13 12:55pm
Comments
PIEBALDconsult at 14-Feb-13 19:54pm
   
You might want to use NumericUpDowns instead.
Jibesh at 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
good
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);
  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.
  Permalink  
v3
Comments
Mostafa Metwally at 14-Feb-13 20:43pm
   
thank you Mr.Jibesh
Jibesh at 14-Feb-13 20:50pm
   
Glad that helps.
Sergey Alexandrovich Kryukov at 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 at 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 at 15-Feb-13 11:42am
   
I see. Yes, I missed it; it wasn't in the original question. Thank you, —SA
Rate this: bad
good
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.
 
—SA
  Permalink  
v2
Comments
Mostafa Metwally at 14-Feb-13 19:19pm
   
i tried what you say and same problem my data comes from ui
Sergey Alexandrovich Kryukov at 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 at 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 at 14-Feb-13 20:05pm
   
What do you mean? Literally "textbox"?! Instead of something like "32332"? —SA
Jibesh at 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 at 14-Feb-13 20:02pm
   
I'm typing a number in the text box to Calculate the total salary
Sergey Alexandrovich Kryukov at 14-Feb-13 20:06pm
   
You (the user) type the string representing a number. Is it a valid string? What is it, for example? —SA
Mostafa Metwally at 14-Feb-13 20:13pm
   
i'm writing a number in the textbox like 10 , 20 , 55 ... etc and then press button
Jibesh at 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 at 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 at 14-Feb-13 20:31pm
   
see my solution. on how to split the text input for a coma separated text.
Mostafa Metwally at 14-Feb-13 20:36pm
   
thank you Mr.Jibesh
Espen Harlinn at 14-Feb-13 19:56pm
   
You can prevent the user from typing in invalid text using the MaskedTextBox Class http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.aspx
Sergey Alexandrovich Kryukov at 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: http://msdn.microsoft.com/en-us/library/57dy4d56.aspx. I show one more thin in Solution 2, please see. —SA
Espen Harlinn at 15-Feb-13 3:44am
   
True, but when I see a decimal I kind of expect huge values ...
Sergey Alexandrovich Kryukov at 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... —SA
Espen Harlinn at 14-Feb-13 19:56pm
   
Good points :-D
Sergey Alexandrovich Kryukov at 14-Feb-13 20:04pm
   
Thank you, Espen. —SA
Jibesh at 14-Feb-13 20:39pm
   
+5 for the proper conversion usage
Sergey Alexandrovich Kryukov at 14-Feb-13 22:07pm
   
Thank you. —SA
Rate this: bad
good
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.
 
—SA
  Permalink  
Comments
Mostafa Metwally at 14-Feb-13 20:16pm
   
thank you Mr.Sergey
Sergey Alexandrovich Kryukov at 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 at 15-Feb-13 3:45am
   
Nice link :-D
Sergey Alexandrovich Kryukov at 15-Feb-13 11:13am
   
Thank you, Espen. —SA

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Guruprasad.K.Basavaraju 350
1 Sergey Alexandrovich Kryukov 226
2 Shai Vashdi 220
3 OriginalGriff 183
4 Tadit Dash 135
0 Sergey Alexandrovich Kryukov 9,255
1 OriginalGriff 5,433
2 Peter Leow 4,100
3 Maciej Los 3,540
4 Abhinav S 3,333


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 14 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid