Error reading file in script task

  • Hi,

    I am reading a file in a script task and inserting data in database. Using the below code.

    char delimiter = '|';

    StreamReader sr = new StreamReader(fileName);

    while (sr.Peek() != -1)

    {

    SqlParameter[] Params = new SqlParameter[14];

    if(!sr.ReadLine().Contains("ErrorHeader"))

    {

    string[] errorRow = sr.ReadLine().Split(delimiter);

    string action = errorRow[3].Substring(2, 1);

    string IdNo = errorRow[3].Substring(3, 10);

    }

    }

    sr.DiscardBufferedData();

    sr.Close();

    sr.Dispose();

    It reads some random lines and will give null pointer at sr.ReadLine().Split(delimiter);

    Sometime it will process 3 sometimes 4 lines .May be I am missing something. Please suggest.

  • Hi

    I prefer ReadLine method until is null returned instead of Peek, please try:

    String FileLine

    while ((FileLine = sr.ReadLine()) != null)

    {

    ...

    }

    Br.

    Mike

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply