0

I am inserting values from a database into a chart.. I am pulling the data from 2 columns, the first column contains String values ( Mixture of letters, numbers and spaces) and the second column contains float values (Just numeric types) . When ever I run the query I keep getting this error: "Input String was not in the correct format" Can some one help me pls?

try { 
    Query = "SELECT * FROM Products;";
    Reader = conn.ExecuteStatement(Query);

    while (Reader.Read()) { 
        this.chart1.Series["Series1"].Points.
        AddXY((Double.Parse(Regex.Replace(Reader.GetString(1),@"[^\d|\.]",""))),
        (Convert.ToDouble(Reader.GetInt32(4))));
    }
}
catch (Exception ex) {
    MessageBox.Show(ex.Message); 
}

conn.CloseConnection();
5
  • Change ex.Message to ex.ToString() and provide us with the full exception. Commented Feb 4, 2014 at 16:25
  • here is the error: System.formatException:Input string was not in a correct format.at System.Number.ParseSingle(String value, NumberStyles options, NumberFormationInfo numfmt) at System.Single.Parse(String s) Commented Feb 4, 2014 at 16:37
  • Are you sure that the columns returned by the SQL query are in the correct order? Commented Feb 4, 2014 at 16:38
  • is solution work for you ??? Commented Feb 4, 2014 at 16:40
  • Do you need to pass in the correct culture to parse the string, as Parse uses the current culture? If your local culture has different requirements for decimals that the data, then you may need to pass a different format provider in, e.g. CultureInfo.InvariantCulture Commented Feb 4, 2014 at 16:44

1 Answer 1

2

This is a data issue. Either Reader.GetString(1) or Reader.GetString(4) is returning something that cannot be parsed or converted to double.

Instead of relying on SELECT * FROM Products, list the column names and be sure to select from the proper columns.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.