Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 未处理的异常:输入字符串的格式不正确_C#_.net - Fatal编程技术网

C# 未处理的异常:输入字符串的格式不正确

C# 未处理的异常:输入字符串的格式不正确,c#,.net,C#,.net,我得到这个错误,虽然还有其他帖子,但我没有得到一个适当的解决我的问题 调试器正在指向此语句 id = Convert.ToInt32(s); 它在开始时运行良好,但现在正在生成错误。下面是完整的函数。作为旁注,我在Visual Studio 2013中遵循N层体系结构 public List<ATMBO> GetDataFromFile() // get data from file and store it into object and pass to BLL !!!! {

我得到这个错误,虽然还有其他帖子,但我没有得到一个适当的解决我的问题

调试器正在指向此语句

 id = Convert.ToInt32(s);
它在开始时运行良好,但现在正在生成错误。下面是完整的函数。作为旁注,我在Visual Studio 2013中遵循N层体系结构

public List<ATMBO> GetDataFromFile() // get data from file and store it into object and pass to BLL !!!!
{
        List<ATMBO> l = new List<ATMBO>();

        // opening stream !!!
        FileStream f = new FileStream("BankClient.txt", FileMode.Open);
        StreamReader sr = new StreamReader(f);

        if (!File.Exists("BankClient.txt"))
        {
            Console.WriteLine("{0} does not exist.", "BankClient.txt");
        }

        // Start reading from file 
        string record=sr.ReadLine();
        //sr.ReadLine();

        while((record = sr.ReadLine()) != null)
        {
            //record = sr.ReadLine();
            // storing data from file to object!!!! 

            string [] data = record.Split(':');
            //Console.WriteLine(data[0]);
            ATMBO bo = new ATMBO();
            string s = (data[0]);
            int id = 0;

            try
            {
                id = Convert.ToInt32(s);
            }
            catch (FormatException e)
            {
                Console.WriteLine("Input string is not a sequence of digits.");
            }
            catch (OverflowException e)
            {
                Console.WriteLine("The number cannot fit in an Int32.");
            }

            bo.ID1 = id;
            bo.Login = data[1];
            bo.Type = data[2];
            string ss = (data[3]);
            int blnc = Convert.ToInt32(ss);
            bo.Balance = blnc;
            bo.Status = data[4];
            bo.Date = data[5];
            bo.Pin = data[6];
            l.Add(bo);
        }

        sr.Close();
        f.Close();
        return l;
    }

您需要在代码中添加一些错误处理,以确保存在可以使用的实际值,例如

string [] data = record.Split(':');
if(data.length < 7)
    Console.WriteLine("Data doesn't contain what was expected");

s
的值是多少?您的
当前文化是什么?调试代码并告诉我们。如果第一行失败,
data[0]
指向
ID
字符串(因为您使用
拆分),显然它不是有效的字符串。您是否尝试跳过此文本文件的第一行?看来你不需要了。@SonerGönül我已经编辑了这篇文章,请审阅!在
字符串s=(数据[0])上设置断点
并查看值是多少。从您发布的内容来看,它看起来应该可以工作,但最快的解决方法是使用调试器。您是否无意中读取了第一行(列标题)?@Rik:不,他通过添加一个伪
ReadLine()
,跳过了它们。这是可行的,但通过遍历数据,它可以正确显示记录,但显示了两次“数据不包含预期的内容" !@EM923在
WriteLine
上设置一个断点,查看
data
record
包含的内容。然后,您的文本文件包含无效行。我不明白为什么数据数组的长度有时小于七行!我调试过了,但是没有正确地得到它@EM923-或者只需打开记事本或您选择的文本文件编辑器,转到缺少的行(提示:它将是一行下的一行,可以正常工作),并查看该行上的数据。如果需要的话,把它一字不差地写在一张纸上,在每一个分号处撕开那张纸,数一数纸的位数。我通过调试发现问题,null是通过流在最后一条记录旁边的两行找到的,这就是为什么它读取两条null记录多于五条记录的原因!我现在该怎么办?我应该在文件中显式地放置null吗?
string [] data = record.Split(':');
if(data.length < 7)
    Console.WriteLine("Data doesn't contain what was expected");
int id;
if(!int.TryParse(s, out id))
    Console.WriteLine("Not a valid id");