Click here to Skip to main content
15,904,288 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am Using StreamReader to import data from Tab Delim Text file to a Data Table called Import Data. For some reasons none of the superScript characters are readable once its imported to the table.

For example If i have a ProductName value "Universal 360° Rotating Finger Ring Holder" in the text file, after import the value becomes like "Universal 360� Rotating Finger Ring Holder" It's same with other characters like " ® , ™ ".

Is there something to do with my code?

What I have tried:

Public Function FillData(ByVal Fpath As String) As Boolean
        Dim XRead As System.IO.StreamReader = New IO.StreamReader(FilePath)
        Dim XLine As String = Nothing
        Dim XSplitLine() As String
        Dim i As Integer = ImportedData.Rows.Count + 1
        Try
            XRead.ReadLine()
            XLine = XRead.ReadLine()
            Do Until XLine Is Nothing

                XLine = i & vbTab & XLine & vbTab & FilePath
                XSplitLine = XLine.Split(CType(vbTab, Char()))

                ImportedData.Rows.Add(XSplitLine)
                XLine = XRead.ReadLine
                i += 1
            Loop
            XRead.Close()

        Catch ex As Exception
            MessageBox.Show("Error")
            Return False
            Exit Function
        End Try

        Return True
    End Function
Posted
Updated 16-Oct-18 1:14am
Comments
CHill60 15-Oct-18 8:47am    
When creating the new streamreader define an encoding that recognises unicode values

1 solution

I disagree with Solution 1. Quote from Encoding.Default Property (System.Text) | Microsoft Docs[^]

Quote:
Different computers can use different encodings as the default, and the default encoding can change on a single computer. If you use the Default encoding to encode and decode data streamed between computers or retrieved at different times on the same computer, it may translate that data incorrectly. In addition, the encoding returned by the Default property uses best-fit fallback to map unsupported characters to characters supported by the code page. For these reasons, using the default encoding is not recommended. To ensure that encoded bytes are decoded properly, you should use a Unicode encoding, such as UTF8Encoding or UnicodeEncoding. You could also use a higher-level protocol to ensure that the same format is used for encoding and decoding.
I would explicitly use
VB
Dim XRead As System.IO.StreamReader = New IO.StreamReader(FilePath, System.Text.Encoding.UnicodeEncoding)
For more information see Encoding Class (System.Text) | Microsoft Docs[^]
 
Share this answer
 
Comments
Alek Massey 16-Oct-18 17:37pm    
Good call, I withdraw my flawed solution.
CHill60 16-Oct-18 18:51pm    
We agree to disagree! This is the beauty of a forum such as this. At least you offered the OP a route to their solution ... nobody else bothered - so thank you
Member 14000611 27-Oct-18 1:02am    
Thank you all for your time and help. Much appreciated.

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