Keep Leading Zeroes In CSV File When Importing To VB.NET

Posted on 2007-07-25
Last Modified: 2013-11-26
I'm importing a CSV file into VB.NET 2005.  I'm storing the CSV file in a dataset, and I want to manipulate that...NOT the original CSV file.  This dataset is then being displayed in a DataGridView or a Messagebox.
The CSV file contains a 3-character field that holds numbers with leading zeros.  Example: 027, 001, 033.  These leading zeros ARE showing when I view the CSV file in Notepad.

The problem is that when I display these values in a Messagebox or DataGridView, it appears that the leading zeros have been dropped.  How can I keep these leading zeros when I import the CSV to a dataset for further manipulation?

PS:  I AM NOT using an Excel spreadsheet, and I am not manipulating the CSV file directly...just the dataset that stores info. from the CSV.  The field in question is called "loc" and it should be 3 characters long, with leading zeros.  Example: 027, 033, 001  They are stored correctly in the actual CSV file.
Question by:redmission
    LVL 48

    Expert Comment

    Are you showing/managing that information as string? If you use the integer it cuts the leading zeroes.
    LVL 2

    Accepted Solution

    If you are managing using a string you can use padleft to put the leading zero(s) in place.

    strSomeName.Padleft(Number of spaces,Char to pad)
    strSomeName.Padleft(3,"0")   This will insert leading zero(s) if the number in the string is less than 3 characters long   12 would be 012 etc....


    Author Comment

    I thought it would all be text since it's a CSV file.  Here's a sample of a record from the CSV:

    101,027,Frances Gavin,3 Fencourt Rd
    101,027,Geo Seabrook,7 Fencourt Rd

    The "loc" field is the one that won't display with the leading zero.  I don't care about the first field (the "id" field) not having leading zeroes, it's the "loc" field that I want with the zeroes.  Here's my VB...This pulls all the CSV info and puts it in a datagridview...but won't show leading zeroes:

    Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
            strAddressesFilePath & ";Extended Properties=Text;"
            Dim objConn As New OleDbConnection(strConn)
            Dim cmd As New OleDbCommand("SELECT * FROM [" & strAddressesFile & "]", objConn)
            Dim objAdapter As New OleDbDataAdapter()
            objAdapter.SelectCommand = cmd
            dsMail = New DataSet
            objAdapter.Fill(dsMail, "Mailshop")
    Me.DataGridView1.DataSource = dsMail.Tables("Mailshop").DefaultView

    Is there a way to manipulat the dataset for that column so that it formats it correctly, or is there something wrong in the CSV file?  Help...this is driving me insane!!! :)  Thanks so much.
    LVL 2

    Expert Comment

    Take a look at this. Work is calling me right now so I will be out of pocket for a bit.


    Author Comment

    drekow:  Previously, I had tried strTheString = Format(strTheString, "000"), and that didn't work!  But your solution of strTheString.PadLeft(3, "000") worked PERFECTLY!  Thanks so much!  Now I can continue with my work.
    LVL 2

    Expert Comment

    Glad I could be of help. Sorry about bailing yesterday. My work is like having a child.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    This video discusses moving either the default database or any database to a new volume.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now