Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8694013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:40:49+00:00 2026-06-13T00:40:49+00:00

I am using CSVReader in my project for reading .csv files in windows service.

  • 0

I am using CSVReader in my project for reading .csv files in windows service.
we pass the .csv file to CSVReader for processing the file and it was working fine.
But recently we decided to save the csv file to Database table and read it from there.
when a user submits an csv file for processing , our aspx page will read the csv file
and converts it to byte array and saves it to database.
and the service will read the table and picks up the byte array and converts it to Filestream.
This stream is passed to CSVReader for further work.
Now it throws error for the last row last column.
Its happening only after saving and reading from Database.
I am getting the following error.
No idea how to fix this.

“The CSV appears to be corrupt near record ‘9’ field ‘3 at position ‘494’. Current raw data : ‘

Hear is the code
Converting file to Byte Array …..

try
{
     fs = new FileStream(filepath, FileMode.Open);
     fsbuffer = new byte[fs.Length];
     fs.Read(fsbuffer, 0, (int)fs.Length);
}

Reading from DB to Byte Array ……

myobject.FileByteArray = ObjectToByteArray(row);

public byte[] ObjectToByteArray(DataRow row)
        {
            if (row["fileBytearray"] == null)
                return null;
            try
            {
                BinaryFormatter bf = new BinaryFormatter();
                System.IO.MemoryStream ms = new System.IO.MemoryStream();

                bf.Serialize(ms, row["fileBytearray"]);
                return ms.ToArray();
            }
    }

Stream fileStream = new MemoryStream(myobject.FileByteArray)
using (CsvReader csv = 
                        new CsvReader(new StreamReader(fileStream, System.Text.Encoding.UTF7), hasHeader, ','))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:40:51+00:00Added an answer on June 13, 2026 at 12:40 am

    I haven’t been able to recreate your issue, so instead, as, without seeing the save and load from beginning to end, I’ll suggest trying the following for retrieval:

    public MemoryStream LoadReportData(int rowId)
    {
    
      MemoryStream stream = new MemoryStream();
    
      using (BinaryWriter writer = new BinaryWriter(stream))
      {
    
        using (DbConnection connection = db.CreateConnection())
        {
          DbCommand selectCommand = "SELECT CSVData FROM YourTable WHERE Id = @rowId";
    
          selectCommand.Connection = connection;
    
          db.AddInParameter(selectCommand, "@rowId", DbType.Int32, rowId);
          connection.Open();
    
          using (IDataReader reader = selectCommand.ExecuteReader(CommandBehavior.SequentialAccess))
          {
    
            while (reader.Read())
            {
    
              int startIndex = 0;
              int bufferSize = 8192;
    
              byte[] buffer = new byte[bufferSize];
    
              long retVal = reader.GetBytes(0, startIndex, buffer, 0, bufferSize);
    
              while (retVal == bufferSize)
              {
    
                writer.Write(buffer);
                writer.Flush();
    
                startIndex += bufferSize;
                retVal = reader.GetBytes(0, startIndex, buffer, 0, bufferSize);
    
              }
    
              writer.Write(buffer, 0, (int)retVal);
            }
    
          }
    
        }
      }
    
      return stream;
    
    }
    

    You’ll need to replace the selectCommand sql and parameters with whatever sql you are using to return the data.

    This method uses a SqlDataReader to sequentially read bytes from the appropriate row (identified by rowId in my example) and column (called CSVData in my example) which should avoid any truncation issues on the way out (it could be that your DataTable object is only returning the first n bytes). The MemoryStream object could be used to resave the CSV file to the file system for testing, or fed straight into your CSVReader.

    If you can post your Save method (where you actually persist the data to the database) then we can check that too to make sure that truncation isn’t happening there, either.

    One other suggestion I can make right now involves your loading the file into a byte array. If you are going to load the file in one go, then you can simply replace:

    try
    {
         fs = new FileStream(filepath, FileMode.Open);
         fsbuffer = new byte[fs.Length];
         fs.Read(fsbuffer, 0, (int)fs.Length);
    }
    

    with

    byte[] fileBytes = File.ReadAllBytes(filepath);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to read csv file twice. but after first reading: using (var csvReader
I am using csv.reader to read file but it giving me whole file. file_data
I am reading a csv file using opencsv. I am ignoring the first line
I am using a CSVReader class that takes a local file as input. But
I'm parsing a big CSV file using csv.DictReader . quotes=open( "file.csv", "rb" ) csvReader=
I am trying to pass a csv file using opencsv.The csv file has three
I am using Sebastien Lorion Reference CSV reader to process my CSV file in
I am using scala 2.7.7, and wanted to parse CSV file and store the
If csv file has rich text in it. Using csv.reader() can the same format
I'm using the CSV module to read a tab delimited file. Code below: z

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.