locked
Unable to convert string to integer data from data reader. RRS feed

  • Question

  • User1864044315 posted
    SqlConnection balance = new SqlConnection();
                balance.ConnectionString = ConfigurationManager.ConnectionStrings["SMART_SCHEDULEConnection"].ToString();
                balance.Open();
                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 + "'";
    
    
    
                // Response.Write(lotcheck1.CommandText);
                balance1.Connection = balance;
                SqlDataReader getbalance = balance1.ExecuteReader();
               
    			getbalance.Read ();
    
    
               string totalsum = getbalance["Total"].ToString();
    
                Response.Write(totalsum);
                int Target = Int32.Parse(Target1);
    			int Total_Output = Int32.Parse(totalsum);
    
                int Total_Balance = Target - Total_Output;
                //Response.Write(Total_Balance);
    
    
                SqlConnection updatebalance = new SqlConnection();
                updatebalance.ConnectionString = ConfigurationManager.ConnectionStrings["SMART_SCHEDULEConnection"].ToString();
                updatebalance.Open();
                SqlCommand updatebalance1 = new SqlCommand();
               updatebalance1.CommandText = " UPDATE Device_Plan SET output='" + Total_Balance + "'" +
    			" WHERE Plan_ID='" + planid + "' AND device='" + device + "' AND process_id='" + processid1 + "'";
    			Response.Write(updatebalance1.CommandText);
               updatebalance1.Connection = updatebalance;
               updatebalance1.ExecuteNonQuery();
    
            }

    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.

    Wednesday, December 14, 2016 1:34 PM

Answers

  • User-691209617 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 14, 2016 1:48 PM
  • User-2057865890 posted

    Hi JBLD_12,

    "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 TryParse.

    reference:

    https://msdn.microsoft.com/en-us/library/b3h1hf19.aspx 

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 15, 2016 2:34 AM

All replies

  • User-691209617 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 14, 2016 1:48 PM
  • User753101303 posted

    Hi,

    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.

    Wednesday, December 14, 2016 1:57 PM
  • User-2057865890 posted

    Hi JBLD_12,

    "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 TryParse.

    reference:

    https://msdn.microsoft.com/en-us/library/b3h1hf19.aspx 

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 15, 2016 2:34 AM
  • User1864044315 posted
     Int32.TryParse(totalsum, out result);

    In this way it's works well """Int32.TryParse(totalsum, out result);""""

    Questions:

    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 Total 

    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.

    Thursday, December 15, 2016 7:32 AM
  • User753101303 posted

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

    Thursday, December 15, 2016 7:45 AM