Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7840069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:39:00+00:00 2026-06-02T15:39:00+00:00

On a ASP.NET 4 website and im getting the following error when trying to

  • 0

On a ASP.NET 4 website and im getting the following error when trying to load data from the database into a GridView.

Unable to translate Unicode character \uD83D at index 49 to specified code page.

I’ve found out that this happens when a data row contains:
Text Text Text

As i understand this text cannot be translated into a valid utf-8 response.

  1. Is that really the reason?

  2. Is there a way to clean the text before loading it into the gridview to prevent such errors?


UPDATE:

I have some progress
I’ve found out I only get this error when I’m using Substring method on a string.
(I’m using substring to show part of the text as a preview to the user).

For example in an ASP.NET Web Form I do this:

String txt = test ;

//txt string can also be created by 
String txt = char.ConvertFromUtf32(116) + char.ConvertFromUtf32(101) +char.ConvertFromUtf32(115) + char.ConvertFromUtf32(116) + char.ConvertFromUtf32(32) + char.ConvertFromUtf32(128148);

// this works ok txt is shown in the webform label.
Label1.Text = txt; 

//length is equal to 7.
Label2.Text = txt.Length.ToString();

//causes exception - Unable to translate Unicode character \uD83D at index 5 to specified code page.
Label3.Text = txt.Substring(0, 6);

I know that .NET string is based on utf-16 which supports surrogate pairs.

When i’m using SubString function I accidently break the surrogate pair and causes the exception.
I found out that I can use StringInfo class:

var si = new System.Globalization.StringInfo(txt);
var l = si.LengthInTextElements; // length is equal to 6.
Label3.Text = si.SubstringByTextElements(0, 5); //no exception!

Another alternative is to just delete the surrogate pairs :

Label3.Text = ValidateUtf8(txt).Substring(0, 3); //no exception!

    public static string ValidateUtf8(string txt)
            {
                StringBuilder sbOutput = new StringBuilder();
                char ch;

                for (int i = 0; i < body.Length; i++)
                {
                    ch = body[i];
                    if ((ch >= 0x0020 && ch <= 0xD7FF) ||
                            (ch >= 0xE000 && ch <= 0xFFFD) ||
                            ch == 0x0009 ||
                            ch == 0x000A ||
                            ch == 0x000D)
                    {
                        sbOutput.Append(ch);
                    }

                }
                return sbOutput.ToString();
            }

Is this really a problem of surrogate pairs?

Which characters use surrogate pairs ? is there a list?

Should I keep support for surrogate pairs? should i go with using StringInfo Class or just delete non valid chars?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T15:39:01+00:00Added an answer on June 2, 2026 at 3:39 pm

    You could try encoding the text to UTF8 first (in the row bound event or something similar). The following code will encode text in UTF8 and remove un-encodable characters.

    private static readonly Encoding Utf8Encoder = Encoding.GetEncoding(
        "UTF-8",
        new EncoderReplacementFallback(string.Empty),
        new DecoderExceptionFallback()
    );
    
    var utf8Text = Utf8Encoder.GetString(Utf8Encoder.GetBytes(text));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ive been trying to convert my asp.net website into a web application following the
I am getting following error for log4Net in Asp.Net 2.0 website. Compiler Error Message:
I am trying to scrape an ASP.NET website but am having trouble getting the
I am getting following error when i am trying to connect Production DB from
I was following this tutorial - Getting Started with an ASP.NET MVC 3 Website
I am getting the following error while browsing my asp.net page deployed in IIS
I am getting the following error while browsing my asp.net page deployed in IIS:
I am getting the following error if I try to run the ASP.NET 4.0
I am getting the following exception when I call the WCF Service from ASP.NET
I'm trying to upgrade my ASP.NET MVC 1 website to version 2. While doing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.