web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Reading CSV File

(0) ShareShare
ReportReport
Posted on by

Hi CRM Kings and Queens,

I have a windows app that reads a CSV file and writes the data into CRM. Since updating the dlls due to the MS deprecation, the program has since stopped to work. Ideally i need help and alternatives with the following script which is falling to read the Excel file and always returns 0

byte[] fileContent = Convert.FromBase64String(attDocument.GetAttributeValue<string>("documentbody"));

                                  CachedCsvReader CSV = new CachedCsvReader(new StreamReader(new MemoryStream(fileContent)), true);

                                  DataImport DI = new DataImport(ref service, attDocument.GetAttributeValue<EntityReference>("objectid").Id, CSV);

                                  DI.ImportData();

I am not a developer and didn't write the script so wont be able to explain why the above code was written in such a way. Ideas and explanations are welcome. And no, there is valid reason why the out of the box import tool is not being used.

Many Thanks,

*This post is locked for comments

I have the same question (0)
  • Jay2014 Profile Picture
    on at
    RE: Reading CSV File

    No errors, its just returning 0 when debugging that line ..

  • PranavShroti Profile Picture
    4,510 on at
    RE: Reading CSV File

    I understood, let me elaborate more, I have suggested one more way of accessing info which you can try.

    Are you sure in your code you are not getting any error?

    Regards,

    Pranav

  • Jay2014 Profile Picture
    on at
    RE: Reading CSV File

    Thanks Pranav, its the reading of the file i am concerned about because it's returning 0 for me ..

                                    CachedCsvReader CSV = new CachedCsvReader(new StreamReader(new MemoryStream(fileContent)), true);

                                     DataImport DI = new DataImport(ref service, attDocument.GetAttributeValue<EntityReference>("objectid").Id, CSV);

  • Suggested answer
    PranavShroti Profile Picture
    4,510 on at
    RE: Reading CSV File

    Hi M.Ncube

    I have written a small function to read from excel file.  It uses a different approach to connect. I have a simple .Net windows app that reads data from Excel/CSV.

    Hope this helps

    public static bool ReadExcel()

           {

            MDIParent.dtExcel = null;

               MDIParent.lstEntitySheets = new List<DataTable>();  

               OpenFileDialog openFileDialog = new OpenFileDialog();

               openFileDialog.ShowDialog();

               openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);

               openFileDialog.Filter = "Microsoft Excel 97-2003 (.xls)|*.xls|Microsoft Excel 2007 (.xlsx)|*.xlsx";

               filePath = openFileDialog.FileName;

               string connectionString = string.Empty;

               if (filePath.EndsWith(".xls"))

                   connectionString = @"provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';"; //for below excel 2007  

               else if (filePath.EndsWith(".xlsx"))

                   connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1';;"; //for above excel 2007  

               else

               {

                   MessageBox.Show("Please choose .xlsx file only.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);

                   return false;

               }

               try

               {

                   OleDbConnection oledbConnection = new OleDbConnection(connectionString);

                   OleDbDataAdapter oledbdataAdapter = new OleDbDataAdapter();

                   OleDbCommand oledbCommand = new OleDbCommand();

                   oledbConnection.Open();

                   MDIParent.dtExcel = oledbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

                   MDIParent.dtExcel.Columns.Add(new DataColumn("IsValid", typeof(bool)));

                   ///READ FILE ROWS

    for (int j = 0; j < MDIParent.dtExcel.Rows.Count; j++)

                   {

                       if (MDIParent.dtExcel.Rows[j]["TABLE_NAME"].ToString().ToLower() == "information$")

                           MDIParent.dtExcel.Rows.Remove(MDIParent.dtExcel.Rows[j]);

                   }

                   oledbConnection.Close();

               }

               catch (Exception ex)

               {

                   if (ex.InnerException != null && !string.IsNullOrEmpty(ex.InnerException.Message))

                   {

                       MessageBox.Show("Could not read the file due to an Error." + Environment.NewLine + "ERROR: " + ex.InnerException.Message);

                       Config.SetLog("Could not read the file due to an Error.");

                       Config.SetLog("ERROR: " + ex.InnerException.Message);

                   }

                   else

                   {

                       MessageBox.Show("Could not read the file due to an Error." + Environment.NewLine + "ERROR: " + ex.Message);

                       Config.SetLog("Could not read the file due to an Error.");

                       Config.SetLog("ERROR: " + ex.Message);

                   }

                   return false;

               }

               return true;

           }

    Regards

    Pranav

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Aric Levin - MVP Profile Picture

Aric Levin - MVP 2 Moderator

#2
MA-04060624-0 Profile Picture

MA-04060624-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans