Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am reading a .csv file. One of the cell contains a value "
VB
2.72E+11

"
When i douuble click the cell the value is 271500000000..I need to store this value in my Dataset how can i achieve this?Please help.

Thanks.
Posted
Comments
Maciej Los 26-Mar-13 9:37am    
VB.NET or VBA? It changes everything...
Please, check the NumberFormat for this cell (General, Numeric, ...).
vidkaat 26-Mar-13 9:47am    
it is vb.net....I stored the cell value in a string. But i get the value as "2.72E+11"...How do i convert this notation to this 271500000000.
Maciej Los 26-Mar-13 9:50am    
Show me your code, where you retrive data fromm csv file.
vidkaat 26-Mar-13 9:52am    
Dim rows As String() = System.Text.Encoding.Default.GetString(buffer).Split(vbLf.ToCharArray())
Dim dteRunDate As Date = Today()

For Each row As String In rows
row = row.Trim()
Dim strColumns As String() = row.Split(",")
Maciej Los 26-Mar-13 9:56am    
Please, next time use "Improve question" option, OK?

1 solution

I suggest you to use OleDb drivers to read comma separated files.

Steps to do:
1) create new windows application project
2) add controls:
- TextBox (Name: TxtFileToLoad) - here you'll define path to csv file
- DataGridView (Name: DataGridView1) - to display data
- Button (Name: BtnLoadData)
3) copy and paste below code:
VB
Private Sub BtnLoadData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnLoadData.Click
    Dim scon As String = String.Empty, scom As String = String.Empty
    Dim con As OleDb.OleDbConnection = Nothing, com As OleDb.OleDbCommand = Nothing, rdr As OleDb.OleDbDataReader = Nothing
    Dim sFileName As String = String.Empty
    Dim dt As Data.DataTable = Nothing, r As DataRow = Nothing
    Try

        sFileName = Me.TxtFileToLoad.Text
        scon = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & IO.Path.GetDirectoryName(sFileName) & ";Extended Properties='Text;HDR=Yes;Schema=schema.ini;';"
        scom = CreateCommand(sFileName)
        con = New OleDb.OleDbConnection(scon)
        con.Open()
        com = New OleDb.OleDbCommand(scom, con)
        rdr = com.ExecuteReader()
        dt = New Data.DataTable
        dt.Load(rdr)
        Me.DataGridView1.DataSource = dt

    Catch ex As OleDb.OleDbException
        MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error...")

    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error...")
    Finally
        If Not rdr Is Nothing Then rdr.Close()
        If Not com Is Nothing Then com.Dispose()
        If Not com Is Nothing Then con.Close() : con.Dispose()
    End Try
End Sub

Function CreateCommand(ByVal sFileName As String) As String
    Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()

    sb.AppendLine("SELECT *")
    sb.AppendLine("FROM " & sFileName & ";")

    Return sb.ToString

End Function

4) define schema.ini file; store it in the same folder where the data file is placed.
More about schema.ini file (Text File Driver): http://msdn.microsoft.com/en-us/library/windows/desktop/ms709353%28v=vs.85%29.aspx[^]
5) Run project

My example data (someData2.csv):
some data 1,271500000000,another data 1, 500
some data 2,381700000000,another data 2, 900
some data 3,211500000000,another data 3, 1500
some data 4,932500000000,another data 4, 300


My example schema:
[someData2.csv]
Format=Delimited(,)
ColNameHeader=FALSE
DecimalSymbol=.
NumberDigits=0
MaxScanRows=0
StartScanRow=0
Col1=Column1 Text Width 30
Col2=Column2 Double
Col3=Column3 Text Width 30
Col4=Column4 Long


DataGridView displays 271500000000 as 271500000000 not 2.72E+11
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900