System.NotSupportedException: No data is available for encoding 1252
Asked Answered
G

7

137

I'm working with a Trust Commerce Tutorial on how to generate a payment token that will allow customers to use the TC Trustee Host payment form. I was given an example on how to retrieve this token by their dev team.

using System;
using System.Net;
using System.IO;
using System.Text;
using System.Collections;
using System.Web;

/** @class TCToken
 * An example class for generating a TrustCommerce Trustee Token
 */
public class TCToken
{

    public static void Main(string [] args)
    {
        string custid = "123456";
        string password = "XXXXXX";
        try {
            // Adapted from http://www.west-wind.com/presentations/dotnetWebRequest/dotnetWebRequest.htm
            string gateway_post_address = "https://vault.trustcommerce.com/trustee/token.php";
            HttpWebRequest req = (HttpWebRequest) WebRequest.Create(gateway_post_address);

            // A sixty second timeout.
            req.Timeout = 60000;

            string post_data = "custid=" + HttpUtility.UrlEncode(custid) +
                                "&password=" + HttpUtility.UrlEncode(password);
            
            req.Method = "POST";
            byte [] buf = System.Text.Encoding.GetEncoding(1252).GetBytes(post_data);
            req.ContentLength = buf.Length;
            req.ContentType = "application/x-www-form-urlencoded";

            Stream s = req.GetRequestStream();
            s.Write(buf, 0, buf.Length);
            s.Close();

            HttpWebResponse rep = (HttpWebResponse) req.GetResponse();
            Encoding enc = System.Text.Encoding.GetEncoding(1252);
            StreamReader rs = new StreamReader(rep.GetResponseStream(), enc);

            string token = rs.ReadToEnd();

            Console.WriteLine(token);

            rep.Close();
            rs.Close();
        } catch (Exception e) {
            Console.WriteLine(e);
        }
    }
}

I made a new console application in visual studio, copied this code, and replaced the username and password with the correct credentials. When I try to run this, I get the following error in the console.

System.NotSupportedException: No data is available for encoding 1252. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method. at System.Text.Encoding.GetEncoding(Int32 codepage) at TCToken.Program.Main(String[] args) in C:\Users\xxxx\source\repos\TCToken\TCToken\Program.cs:line 29

I've tried to google this error and most of the responses are a little above my understanding. I'm certainly not a C# expert.

Guava answered 14/6, 2018 at 13:1 Comment(7)
Is your console using .NET Framework or .NET Core? I think on Core you need to install the System.Text.Encoding.CodePages NuGet packageTentage
.NET Core 2.0 I did install that package via nuget and rebuilt my solution. I got the same error.Guava
I forgot that you need to follow three steps as described in msdn.microsoft.com/en-us/library/…Tentage
If you really don't need 1252 encoding, then use UTF8Freestone
@Tentage that did it!Guava
@Tentage if you make your comment into an answer then I'll accept it.Guava
Done as you suggested.Tentage
T
193

.NET Core supports only ASCII, ISO-8859-1 and Unicode encodings, whereas .NET Framework supports much more.

However, .NET Core can be extended to support additional encodings like Windows-1252, Shift-JIS, GB2312 by registering the CodePagesEncodingProvider from the System.Text.Encoding.CodePages NuGet package.

After the NuGet package is installed the following steps as described in the documentation for the CodePagesEncodingProvider class must be done to register the provider:

  1. Add a reference to the System.Text.Encoding.CodePages.dll assembly to your project.
  2. Retrieve a CodePagesEncodingProvider object from the static Instance property.
  3. Pass the CodePagesEncodingProvider object to the Encoding.RegisterProvider method.

EncodingProvider registration code example:

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Tentage answered 15/6, 2018 at 12:33 Comment(3)
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); This is how you add the reference to the codeRoofing
Thanks for mentioning the right library to use for .NET Core apps. fyi - the answer from dawidg below removes the need to specifically reference the .dll assembly.Oech
I am not registering the provider, just straight up using the provider like this: CodePagesEncodingProvider.Instance.GetEncoding(852)Gunsmith
U
209

What ckuri said. Just to be clear, you need the following line of code before opening the stream (steps 2,3):

System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

ExcelDataReader - Important note on .NET Core

By default, ExcelDataReader throws a NotSupportedException "No data is available for encoding 1252." on .NET Core.

To fix, add a dependency to the package System.Text.Encoding.CodePages and then add code to register the code page provider during application initialization (f.ex in Startup.cs):

System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

This is required to parse strings in binary BIFF2-5 Excel documents encoded with DOS-era code pages. These encodings are registered by default in the full .NET Framework, but not on .NET Core.

Unhallow answered 24/9, 2019 at 6:56 Comment(0)
T
193

.NET Core supports only ASCII, ISO-8859-1 and Unicode encodings, whereas .NET Framework supports much more.

However, .NET Core can be extended to support additional encodings like Windows-1252, Shift-JIS, GB2312 by registering the CodePagesEncodingProvider from the System.Text.Encoding.CodePages NuGet package.

After the NuGet package is installed the following steps as described in the documentation for the CodePagesEncodingProvider class must be done to register the provider:

  1. Add a reference to the System.Text.Encoding.CodePages.dll assembly to your project.
  2. Retrieve a CodePagesEncodingProvider object from the static Instance property.
  3. Pass the CodePagesEncodingProvider object to the Encoding.RegisterProvider method.

EncodingProvider registration code example:

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Tentage answered 15/6, 2018 at 12:33 Comment(3)
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); This is how you add the reference to the codeRoofing
Thanks for mentioning the right library to use for .NET Core apps. fyi - the answer from dawidg below removes the need to specifically reference the .dll assembly.Oech
I am not registering the provider, just straight up using the provider like this: CodePagesEncodingProvider.Instance.GetEncoding(852)Gunsmith
Z
40

nuget:
Install-Package System.Text.Encoding.CodePages -Version 5.0.0

code:
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

Zamindar answered 25/6, 2021 at 10:57 Comment(2)
I came here for the error message and I stayed for these two lines of code. The rest is just chatter. This is the answer that works for me. Brilliant!!!Preterite
@Preterite - You are a true master of stackoverflow's copy&paste philosophy, you don't care why the issue exists, just give me code to copy&paste - mission accomplishedJovanjove
I
11

Solution:

  1. Add the System.Text.Encoding.CodePages Package to your project.

  2. Write this code in your program:

     System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
    
Intubate answered 25/7, 2022 at 8:39 Comment(0)
S
10

I was experiencing similar issue when I was trying to read and convert xlsx file to DataTable. I found out that encoding 1252 are not default in .NET Core therefore I had to separately add NuGet package for the same.

Below is the method where I convert the data from memory stream.

private static DataTableCollection ExcelToDataTable(MemoryStream stream)
{
    System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

    using (var reader = ExcelReaderFactory.CreateReader(stream))
    {
        var result = reader.AsDataSet(new ExcelDataSetConfiguration()
        {
            ConfigureDataTable = (data) => new ExcelDataTableConfiguration()
            {
                UseHeaderRow = true
            }
        });
        
        return result.Tables;
    }
}

I referenced the Encoder from nuGet Package at the start of the method and it worked fine for me. This answer is late but might help people who are reading data from streams.

System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
Syncrisis answered 22/7, 2021 at 5:10 Comment(0)
H
4

I've installed the library System.Text.Encoding.CodePages like other posts said and this code worked for me:

System.Text.Encoding.RegisterProvider(
    System.Text.CodePagesEncodingProvider.Instance);
Encoding srcEncoding = Encoding.GetEncoding(1251);
using (var reader = new StreamReader(@"D:\someFile.csv", encoding: srcEncoding))
{
    List<string> listA = new List<string>();                
    while (!reader.EndOfStream)
    {
         var line = reader.ReadLine();
         var values = line.Split(';');
         listA.Add(values[0]);                    
    }
}
Hooded answered 6/3, 2022 at 13:52 Comment(0)
M
1

This solutions works for me:

System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

pointed by dawidg (https://stackoverflow.com/users/482649/dawidg) up there.

however I notice that when I use entity framework Core 6.0.5 with ExecuteSqlRaw it also fixed the error.

using (DbContext db = new())
{
    db.Database.ExecuteSqlRaw(";");
}

After that the error also disappears. It's fun but it works.

Mirk answered 31/8, 2023 at 15:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.