Link to home
Start Free TrialLog in
Avatar of Wilder1626
Wilder1626Flag for Canada

asked on

VB.NET - Error: No data is available for encoding 1252.

Hi All

I'm starting this new VB.NET project but i'm now facing an error:
No data is available for encoding 1252.

System.NotSupportedException
  HResult=0x80131515
  Message=No data is available for encoding 1252. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.
  Source=System.Private.CoreLib
  StackTrace:
   at System.Text.Encoding.GetEncoding(Int32 codepage)
   at ExcelDataReader.ExcelReaderConfiguration..ctor()
   at ExcelDataReader.ExcelReaderFactory.CreateReader(Stream fileStream, ExcelReaderConfiguration configuration)
   at Application1.RateConvertor.btnBrowser_Click(Object sender, EventArgs e) in C:\Users\...\RateConvertor.vb:line 23
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, WM msg, IntPtr wparam, IntPtr lparam)
   at Interop.User32.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.Interop.Mso.IMsoComponentManager.FPushMessageLoop(UIntPtr dwComponentID, msoloop uReason, Void* pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(msoloop reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(msoloop reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(ApplicationContext context)
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
   at Application1.My.MyApplication.Main(String[] Args) in :line 83
 
  This exception was originally thrown at this call stack:
    [External Code]
    Application1.RateConvertor.btnBrowser_Click(Object, System.EventArgs) in RateConvertor.vb
    [External Code]
 

Open in new window

Would you know what's causing this?

For that below code, i have installed: ExcelDataReader version 5.0 and ExcelDataReader.DataSet version 3.6.0

Code:
Imports System.IO
Imports ExcelDataReader
 
Public Class RateConvertor
 
    Private Sub RateConvertor_Load(sender As Object, e As EventArgs) Handles MyBase.Load
 
 
    End Sub
 
    Private Sub cboSheet_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboSheet.SelectedIndexChanged
        Dim dt As DataTable = tables(cboSheet.SelectedItem.ToString())
        DataGridView1.DataSource = dt
 
    End Sub
    Dim tables As DataTableCollection
    Private Sub btnBrowser_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
        Using ofd As OpenFileDialog = New OpenFileDialog() With {.Filter = "Excel 97-2003 Workbook|*.xls|Excel Workbook|*.xlsx"}
            If ofd.ShowDialog() = DialogResult.OK Then
                txtFileName.Text = ofd.FileName
                Using stream = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read)
                    Using reader As IExcelDataReader = ExcelReaderFactory.CreateReader(stream)
                        Dim result As DataSet = reader.AsDataSet(New ExcelDataSetConfiguration() With {
                                                                 .ConfigureDataTable = Function() New ExcelDataTableConfiguration() With {
                                                                 .UseHeaderRow = True}})
                        tables = result.Tables
                        cboSheet.Items.Clear()
                        For Each table As DataTable In tables
                            cboSheet.Items.Add(table.TableName)
                        Next
                    End Using
                End Using
            End If
 
        End Using
 
    End Sub
End Class

Open in new window


Thanks for your help.
Avatar of Wilder1626
Wilder1626
Flag of Canada image

ASKER

Not sure if this can also helpUser generated image

I found the solution.

I only had to add an extra line of code just before the error line 22
I've added:
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instanc

Open in new window

So it becomes:
Private Sub btnBrowser_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
        Using ofd As OpenFileDialog = New OpenFileDialog() With {.Filter = "Excel Workbook|*.xlsx|Excel 97-2003 Workbook|*.xls"}
            If ofd.ShowDialog() = DialogResult.OK Then
                txtFileName.Text = ofd.FileName
                System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance)
                Using stream = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read)
                    Using reader As IExcelDataReader = ExcelReaderFactory.CreateReader(stream)
                        Dim result As DataSet = reader.AsDataSet(New ExcelDataSetConfiguration() With {
                                                                 .ConfigureDataTable = Function() New ExcelDataTableConfiguration() With {
                                                                 .UseHeaderRow = True}})
                        tables = result.Tables
                        cboSheet.Items.Clear()
                        For Each table As DataTable In tables
                            cboSheet.Items.Add(table.TableName)
                        Next
                    End Using
                End Using
            End If
 
        End Using
 
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Alfredo Luis Torres Serrano
Alfredo Luis Torres Serrano
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Alfredo Luis Torres Serrano 

Thanks for the info. I keep on learning now that i have dropped VB6.

Is that Visual Basic or Visual C# or Visual C+?
I don't have any public void in my project. So probably C.

I'm using VB - BASIC programming

Hi

  The code is on C# but the nuget package can be used for any languaje

Hope this helps
Thanks for the information. It can be helpful later.