Last post Dec 15, 2016 07:45 AM by PatriceSc
Dec 14, 2016 01:34 PM|JBLD_12@hotmail.com|LINK
SqlConnection balance = new SqlConnection();
balance.ConnectionString = ConfigurationManager.ConnectionStrings["SMART_SCHEDULEConnection"].ToString();
SqlCommand balance1 = new SqlCommand();
balance1.CommandText = "SELECT SUM(qty_out) AS Total FROM Machine_Input " +
"WHERE device='" + device + "' AND Plan_ID='" + planid + "'" +
"AND process_id='"+ processid1 + "'";
balance1.Connection = balance;
SqlDataReader getbalance = balance1.ExecuteReader();
string totalsum = getbalance["Total"].ToString();
int Target = Int32.Parse(Target1);
int Total_Output = Int32.Parse(totalsum);
int Total_Balance = Target - Total_Output;
SqlConnection updatebalance = new SqlConnection();
updatebalance.ConnectionString = ConfigurationManager.ConnectionStrings["SMART_SCHEDULEConnection"].ToString();
SqlCommand updatebalance1 = new SqlCommand();
updatebalance1.CommandText = " UPDATE Device_Plan SET output='" + Total_Balance + "'" +
" WHERE Plan_ID='" + planid + "' AND device='" + device + "' AND process_id='" + processid1 + "'";
updatebalance1.Connection = updatebalance;
Error : Additional information: Input string was not in a correct format. (At this location >>"int Total_Output = Int32.Parse(totalsum);")
I need your guys Help on this issue.
Dec 14, 2016 01:48 PM|codemovement.pk|LINK
use trim like this
int Total_Output = Int32.Parse(totalsum.Trim());
or try Int32.TryParse()
by the way what it is printing when you are doing response.write on the page
Dec 14, 2016 01:57 PM|PatriceSc|LINK
And the value found in this string is? You are sure this is an integer. Could it be that you have a decimal point in this string?
My personal preference is to avoid uneeded conversion ie what is returned by getbalance["Total"] should just have the right type (maybe decimal depending on your db side column ?) so I would just use this value rather than convert this to a string and then
convert it back to an integer.
Dec 15, 2016 02:34 AM|Chris Zhao|LINK
"totalsum" may not be int. You could use a try/catch block and handle the FormatException exception if the conversion is unsuccessful. You could also replace the call to the conversion method with a call to a
Dec 15, 2016 07:32 AM|JBLD_12@hotmail.com|LINK
Int32.TryParse(totalsum, out result);
In this way it's works well """Int32.TryParse(totalsum, out result);""""
1) Why the previous code didn't work? and It's work with simple changer code?
2) What is the root cause?
My Guessing :
SELECT SUM(qty_out) AS
That "Total" it's not a column, the field those query created after SUM, I give a name AS Total, so I suspect because of that I getting this error ""Input string was not in a correct format"".
By The way...Thanks guys,,,,,,But I need you guys in more my thread to fix........because currently I'm developing scheduling system in ASP.net, so I facing many errors and can't understand what was the actual issue is.
Dec 15, 2016 07:45 AM|PatriceSc|LINK
This function never fails. It returns a boolean to tell if the conversion fails or not. The result you get is really correct? I believe the change is that now you are just IGNORING the error that STILL happens.
For the root cause don't try to guess. It means that the string can't be converted to an integer so look at the string value to understand what doesn't match (my guess is that you have a decimal point and that Parse fails rather than truncating the value).
Even if the reader expose all columns as "object", it should use the proper type under the hood (ie the sum of a decimal column is a decimal for example). Here you are using ToString to convert that value to a string and then Parse/TryParse to convert it
back to an integer.
My personal preference would be to check which type it is and just use for example (if this is a decimal) :
decimal totalsum = (decimal)getbalance["Total"];
usually you try to convert values to string when you are about to show them to the user (ie when you REALLY need to do that).