HI guys, quick (and perhaps stupid) question. Basically I'm getting some values out of a database, converting them into string and store them in dedicated input fields:

if (reader.HasRows) { //because there should be only 1 row to read, otherwise use while
            reader.Read();            
            rent.Value = Convert.ToString(reader["Rent"]);
            car.Value = Convert.ToString(reader["Car"]);
            bills.Value = Convert.ToString(reader["Bills"]);
            food.Value = Convert.ToString(reader["Food"]);
            //expenseComment.Value = Convert.ToString(reader[""]);
        }
        reader.Close();
        hookUp.Close();

I have an extra input field <input type="text" id="total" runat="server" readonly> where I'd like to store the sum of the above values so I created a decimal variable private decimal expensesTotal; to store all the values in it - rent, car, bills and food. I converted all the values of the above input fields into decimal values like so

expensesTotal = Decimal.Parse(rent.Value) + Decimal.Parse(car.Value) + Decimal.Parse(bills.Value) + Decimal.Parse(food.Value);

and then converted the results back into string and save it onto the total input field like so:

total.Value = Convert.ToString(expensesTotal);

but ASP.NET returns an error:

Input string was not in a correct format.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error: 


Line 50:         reader.Close();
Line 51:         hookUp.Close();
Line 52:         expensesTotal = Decimal.Parse(rent.Value) + Decimal.Parse(car.Value) + Decimal.Parse(bills.Value) + Decimal.Parse(food.Value);
Line 53:         total.Value = Convert.ToString(expensesTotal);
Line 54:     }

Source File: c:\Users\antonio.borrillo\Documents\Visual Studio 2012\WebSites\Expenses\Overview.aspx.cs    Line: 52 

Stack Trace: 


[FormatException: Input string was not in a correct format.]
   System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +10725735
   System.Number.ParseDecimal(String value, NumberStyles options, NumberFormatInfo numfmt) +172
   System.Decimal.Parse(String s) +25
   Overview.Page_Load(Object sender, EventArgs e) in c:\Users\antonio.borrillo\Documents\Visual Studio 2012\WebSites\Expenses\Overview.aspx.cs:52
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
   System.Web.UI.Control.OnLoad(EventArgs e) +92
   System.Web.UI.Control.LoadRecursive() +54
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772

I'm not sure what I'm doing wrong, I mean it's OK to convert the values of an input field - which is allegedly a string - into a decimal providing the string contains a number, like 22.45 etc. I mean what it seems to fail here is when I add up the newly converted decimal...

Recommended Answers

All 2 Replies

Ah...I had an idea. Could it be that I get the error because some of the input fields contain an empty string and therefore I'm trying to convert an empty string into a decimal? Just an idea.

It is always a good programming habbit to check for null values. Nulls can mess your output so much and try for hours to solve it.
So you need to check your inputs against null (string.empty) and if so give them a 0 value. Your sum will be correct and asp.net will not complain.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.