0

I am getting this error though there are other posts as well but I am not getting a proper solution for my problem.

Debugger is pointing to this statement

 id = Convert.ToInt32(s);

It works fine at beginning but now it is generating error. Following is the complete function. As a side note I am following N-tier architecture in Visual Studio 2013.

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;
    }

Contents of my BankClient.txt file:

ID:Name:Type:Balance:Status:Date:Pin
00:Admin:Savings:500:Active:1/11/2014:111
01:Nabeel:Savings:0:Active:1/11/2014:222
02:Asad:Current:600:Active:2/11/2014:333
03:Aqsa:Current:-300:Active:3/11/2014:ABC
04:Umer:Savings:1000:Active:4/11/2014:444            
05:Ali:Savings:1000:Active:4/11/2014:555 
9
  • 4
    What is the value of s and what is your CurrentCulture? Debug your code and tell us. If this fails on your first line, data[0] points to ID string (since you spliting with :) and clearly it is not a valid string. Did you try to skip your first line on this text file? Looks like you don't need it. Commented Nov 4, 2014 at 7:59
  • @SonerGönül i have edited the the post please review ! Commented Nov 4, 2014 at 8:00
  • 2
    Put a breakpoint on string s = (data[0]); and see what the value is. From what you've posted it looks like it should work, but the quickest way to figure it out is to use the debugger. Commented Nov 4, 2014 at 8:01
  • 2
    Are you accidentally reading the first line (the column headers)? Commented Nov 4, 2014 at 8:03
  • 1
    @Rik: No, he skips them by adding a dummy ReadLine(). Commented Nov 4, 2014 at 8:09

1 Answer 1

1

You need to add some error handling to your code to make sure there are actual values you can work with, such as

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

Better yet, instead of Convert.ToInt32 you can use TryParse

int id;
if(!int.TryParse(s, out id))
    Console.WriteLine("Not a valid id");
Sign up to request clarification or add additional context in comments.

5 Comments

This is working, but by traversing data, it is displaying records properly but two times it displays "Data doesn't contain what was expected" !
@EM923 Set a breakpoint on that WriteLine and see what data and record contains then, your text file contains invalid lines.
I am not getting why data array has less length then seven sometimes ! I debugged but not getting it properly !
@EM923 - How To Set a Breakpoint or just open Notepad or the text file editor of your choice, go to the missing line (hint: it will be the one directly below a line that works) and have a look at the data on that line. If needed write it out verbatim onto a peice of paper, and rip that paper up at every semi-colon and count the number of bits of paper.
I find the problem by debugging , null is found by stream two lines next to last record, that is why it is reading two null records more then five records ! What should i do now ? should i place null in file explicitly ?

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.