0

We have an app we use to manage changes to our VOIP system. It uses an upload of a CSV that's built by a custom export out of the CISCO system that controls the phones. It was failing, I think, on a bad row in the .csv file where there was a "-" character, that is removing the character allowed the file to process all the way through. The method that reads the file is fed by a StreamReader and the StaticConfig object is the entity we store the record for each line in. This is the method that read the rows of the .csv:

   while ((LineBuffer = sr.ReadLine()) != null)
                {
                    // loop through all lines found in file
                    string[] tmpString = LineBuffer.Split(Convert.ToChar(","));
                    if (tmpString[0] != "")
                    {
                        StaticConfig staticEntry = new StaticConfig();
                        int extension;
                        if (Int32.TryParse(tmpString[0].Trim(), out extension))
                        {
                            staticEntry.DirectoryNumber = extension;
                            staticEntry.Building = tmpString[9].Trim();
                            staticEntry.LongDistance = (tmpString[7].Trim() == "1");
                            staticEntry.ClassRestricted = (tmpString[6].Trim() == "1");
                            staticEntry.VoiceMail = (tmpString[8].Trim() == "1");
                            staticEntry.Location = tmpString[5].Trim();
                            staticEntry.UserFName = tmpString[1].Trim();
                            staticEntry.UserLName = tmpString[2].Trim();
                            staticEntry.UserID = tmpString[3].Trim();
                            staticEntry.EmployeeNumber = tmpString[4].Trim();
                            staticEntry.UploadDate = uploadTime;

                            //Get the locationID
                            var LocationID = (from l in voipDB.Locations
                                              where l.LocationName == staticEntry.Building
                                              select l.LocationID).FirstOrDefault();

                            if (LocationID != Guid.Empty)
                                staticEntry.LocationID = LocationID;
                            else
                            {
                                Location newLocation = new Location();
                                newLocation.LocationID = Guid.NewGuid();
                                newLocation.LocationName = staticEntry.Building;
                                newLocation.IsLocked = false;
                                newLocation.IsSubmitted = false;

                                voipDB.AddToLocations(newLocation);
                                staticEntry.LocationID = newLocation.LocationID;
                                voipDB.SaveChanges();
                            }

                            voipDB.StaticConfigs.AddObject(staticEntry);
                            processed++;
                        }
                    }
                }

It makes no sense to me though that this would fail on a "-" character though so I feel like I'm missing something. Looking at the MSDN write ups for .Split and Convert.ToChar I don't see anything that would fail. Can anyone tell me why it'd fail or where? Any useful sources of information regarding this? Many thanks in advance!

Ischade
  • 127
  • 2
  • 13
  • Can you provide a minimal CSV example that fails? It's possible that you have a quoted value containing a comma, which this code would not handle correctly since it's simply splitting on commas with no regard to where they occur within each line. See [this answer](http://stackoverflow.com/a/769713/3212415) for more robust CSV parsing. – Troy Gizzi Oct 10 '14 at 23:14
  • You say that this code fails, but you don't give a clear explanation on how it fails. Do you get an error message? If yes what is this message? Do you have try/catch around these lines? – Steve Oct 10 '14 at 23:17
  • The's the problem. It's not giving me any sort of error message. Just reading less rows than it ought. In some cases by hundreds with no clear sign of why it was failing on any given row. After removing this row it read all the lines of the .csv correctly: (ext),Vacant,- RM D22,,,D22,1,0,0,(school) – Ischade Oct 10 '14 at 23:32
  • I can get you a sample csv as well but I actually don't know how to attach it here? – Ischade Oct 10 '14 at 23:34
  • You need that every row is at least composed by 10 substrings (you use the index 9) are you sure that your rows are all composed of 10 substrings? – Steve Oct 10 '14 at 23:34
  • Just upload it to a sharing site like GDrive, OneDrive or DropBox and put the link here – Steve Oct 10 '14 at 23:35
  • Is it possible that an exception is being thrown somewhere inside and your are swallowing it higher up the call chain? – Keith Payne Oct 11 '14 at 01:36
  • Another possibility is that the first `TryParse` is failing and there is no `else`. You could throw an exception instead of doing nothing. – Keith Payne Oct 11 '14 at 01:39
  • Sadly it's not inside of try/catch block to be scooped up by something without being handled. All of the rows ought be composed of 10 substrings but the loop isn't throwing an out of range exception if that's happening and as far as I know that ought happen. – Ischade Oct 12 '14 at 03:25

0 Answers0