Home > Software engineering >  How to fix the coding error 'input string was not in a correct format'
How to fix the coding error 'input string was not in a correct format'

Time:03-26

the error message when i launch my program

Here are the full details of my code:

    public partial class Form1 : Form
    {
        List<Sales> sales = new List<Sales>();
        BindingSource bs = new BindingSource();
        public Form1()
        {
            InitializeComponent();
            LoadCSV();
            bs.DataSource = sales;
            dgvSales.DataSource = bs;
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
    
        }
    
        private void LoadCSV()
        {
            string filePath = @"c:\Users\demo\Task3_shop_data.csv";
            List<string> lines = new List<string>();
            lines = File.ReadAllLines(filePath).ToList();
            foreach (string line in lines)
            {
                List<string> items = line.Split(',').ToList();
                Sales s = new Sales();
                s.TextBook = items[0];
                s.Subject = items[1];
                s.Seller = items[2];
                s.Purchaser = items[3];
                s.purchasedPrice = float.Parse(items[4]);
                s.SalePrice = items[6];
                s.Rating = items[7];
                sales.Add(s);
            }
        }
    }
}

my sales class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MichaelSACU301task3
{
internal class Sales
{
public string TextBook { get; set; }
public string Subject { get; set; }
public string Seller { get; set; }
public string Purchaser { get; set; }
public float purchasedPrice { get; set; }
public string SalePrice { get; set; }
public string Rating { get; set; }
}
}

I tried launching it but the error message keeps appearing can someone please help me fix this problem.

CodePudding user response:

Use float.TryParse prior to assigning to purchasedPrice property, if the value can not be converted remember it in a list. In the example below the code to read file data is in a separate class which returns a list of sales and a list of int which is used to remember invalid lines where purchasedPrice data is invalid. You should also consider validating other data and also ensure proper amount of data after performing the line split.

public class FileOperations
{
    public static (List<Sales>, List<int>) LoadSalesFromFile()
    {
        List<Sales> sales = new List<Sales>();
        List<int> InvalidLine = new List<int>();

        string filePath = @"c:\Users\demo\Task3_shop_data.csv";
        List<string> lines = File.ReadAllLines(filePath).ToList();

        for (int index = 0; index < lines.Count; index  )
        {
            var parts = lines[0].Split(',');
            // validate purchase price
            if (float.TryParse(parts[4], out var purchasePrice))
            {
                Sales s = new Sales();
                s.TextBook = parts[0];
                s.Subject = parts[1];
                s.Seller = parts[2];
                s.Purchaser = parts[3];
                s.purchasedPrice = purchasePrice;
                s.SalePrice = parts[6];
                s.Rating = parts[7];
                sales.Add(s);
            }
            else
            {
                // failed to convert purchase price
                InvalidLine.Add(index);
            }
        }

        return (sales, InvalidLine);
    }
}

Call the above code in your form

var (salesList, invalidLines) = FileOperations.LoadSalesFromFile();
if (invalidLines.Count > 0)
{
    // use to examine bad lines in file
}
else
{
    // uses sales list
}

CodePudding user response:

the error sis probably due the impossiability of float.Parse() parse the items[4] in float you may track value of items[4] using brake point in VS

  • Related