Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Comma with in CSV file

Comma with in CSV file

Scheduled Pinned Locked Moved ASP.NET
databasecsharpasp-netquestion
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Prajin
    wrote on last edited by
    #1

    Dear Developers, I'm importing CSV file to sql DB using ASP.net. It works fine. But ,If a Cell contain a value like john,inc. CSV take it as two values as john and inc But i want in a single value as john,inc .. How can i do thatThanks in Regards, Prajin

    C 2 Replies Last reply
    0
    • P Prajin

      Dear Developers, I'm importing CSV file to sql DB using ASP.net. It works fine. But ,If a Cell contain a value like john,inc. CSV take it as two values as john and inc But i want in a single value as john,inc .. How can i do thatThanks in Regards, Prajin

      C Offline
      C Offline
      Chetan Patel
      wrote on last edited by
      #2

      For this kind of value you have to use ' or " to enclose the Value Ex// 1,'john,store',aroda

      Best Regards, Chetan Patel

      P 1 Reply Last reply
      0
      • C Chetan Patel

        For this kind of value you have to use ' or " to enclose the Value Ex// 1,'john,store',aroda

        Best Regards, Chetan Patel

        P Offline
        P Offline
        Prajin
        wrote on last edited by
        #3

        Hi Chetan, This did not work when I read "1,'john,store',aroda" from file and split it on comma to convert to a datatable, "john" and "store" are coming in different columns. I want this in same column. Regards, Prajin

        V 1 Reply Last reply
        0
        • P Prajin

          Hi Chetan, This did not work when I read "1,'john,store',aroda" from file and split it on comma to convert to a datatable, "john" and "store" are coming in different columns. I want this in same column. Regards, Prajin

          V Offline
          V Offline
          varshavmane
          wrote on last edited by
          #4

          Can u please post the code??

          P 1 Reply Last reply
          0
          • V varshavmane

            Can u please post the code??

            P Offline
            P Offline
            Prajin
            wrote on last edited by
            #5

            try { memStream = new MemoryStream(fileImport.FileBytes); reader = new StreamReader((Stream)memStream); int nCurrPosition = 0; int nNoOfCharactersToBeRead = Convert.ToInt32(reader.BaseStream.Length) - nCurrPosition; szRawString = ""; if (nNoOfCharactersToBeRead >= 0) { reader.BaseStream.Seek(0, SeekOrigin.Begin); char[] vBuffer = new char[nNoOfCharactersToBeRead]; reader.Read(vBuffer, 0, nNoOfCharactersToBeRead); if (vBuffer.Length != 0) { szRawString = ReArrangeStringEx(vBuffer); } } if (szRawString != "") { szRawString = szRawString.Replace("\r", ""); string[] rows = szRawString.Split("\n".ToCharArray()); for (int j = 0; j < rows.Length; j++) { string[] columns = rows[j].Split(','); if (dtCustomer.Columns.Count == columns.Length) { dtCustomer.Rows.Add(columns); } } } reader.Close(); memStream.Close();

            J V 2 Replies Last reply
            0
            • P Prajin

              try { memStream = new MemoryStream(fileImport.FileBytes); reader = new StreamReader((Stream)memStream); int nCurrPosition = 0; int nNoOfCharactersToBeRead = Convert.ToInt32(reader.BaseStream.Length) - nCurrPosition; szRawString = ""; if (nNoOfCharactersToBeRead >= 0) { reader.BaseStream.Seek(0, SeekOrigin.Begin); char[] vBuffer = new char[nNoOfCharactersToBeRead]; reader.Read(vBuffer, 0, nNoOfCharactersToBeRead); if (vBuffer.Length != 0) { szRawString = ReArrangeStringEx(vBuffer); } } if (szRawString != "") { szRawString = szRawString.Replace("\r", ""); string[] rows = szRawString.Split("\n".ToCharArray()); for (int j = 0; j < rows.Length; j++) { string[] columns = rows[j].Split(','); if (dtCustomer.Columns.Count == columns.Length) { dtCustomer.Rows.Add(columns); } } } reader.Close(); memStream.Close();

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #6

              Well you're not operating on this as a CSV (which would honour the quotes as a text identifier) - you're simply splitting the string using comma: string[] columns = rows[j].Split(','); If you want to roll your own code to read from a CSV then you'll need to build something into this line above that honours quotes as a text identifier.

              --- How to get answers to your questions[^]

              1 Reply Last reply
              0
              • P Prajin

                try { memStream = new MemoryStream(fileImport.FileBytes); reader = new StreamReader((Stream)memStream); int nCurrPosition = 0; int nNoOfCharactersToBeRead = Convert.ToInt32(reader.BaseStream.Length) - nCurrPosition; szRawString = ""; if (nNoOfCharactersToBeRead >= 0) { reader.BaseStream.Seek(0, SeekOrigin.Begin); char[] vBuffer = new char[nNoOfCharactersToBeRead]; reader.Read(vBuffer, 0, nNoOfCharactersToBeRead); if (vBuffer.Length != 0) { szRawString = ReArrangeStringEx(vBuffer); } } if (szRawString != "") { szRawString = szRawString.Replace("\r", ""); string[] rows = szRawString.Split("\n".ToCharArray()); for (int j = 0; j < rows.Length; j++) { string[] columns = rows[j].Split(','); if (dtCustomer.Columns.Count == columns.Length) { dtCustomer.Rows.Add(columns); } } } reader.Close(); memStream.Close();

                V Offline
                V Offline
                varshavmane
                wrote on last edited by
                #7

                I suppose u first split your string by ' and then split it by comma. Hope it works.

                1 Reply Last reply
                0
                • P Prajin

                  Dear Developers, I'm importing CSV file to sql DB using ASP.net. It works fine. But ,If a Cell contain a value like john,inc. CSV take it as two values as john and inc But i want in a single value as john,inc .. How can i do thatThanks in Regards, Prajin

                  C Offline
                  C Offline
                  Chetan Patel
                  wrote on last edited by
                  #8

                  for (int j = 0; j < rows.Length; j++) { string[] columns = rows[j].Split(','); if (dtCustomer.Columns.Count == columns.Length) { dtCustomer.Rows.Add(columns); } } change your code like this arrTemp[]=rows[j].Split("'") you get three parts 1,'abc,def',asd as 1, abc,def ,asd now add this like this for (int j = 0; j < rows.Length; j++) { arrTemp[]=rows[j].Split("'") dtCustomer.Rows.Add(arrTemp[0].subString(1,arrTemp[0].Length-1); dtCustomer.Rows.Add(arrTemp[1]); dtCustomer.Rows.Add(arrTemp[2].subString(2); }

                  Best Regards, Chetan Patel

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups