web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

DataContractResolver to return a non-null value for name 'StringBuilder' and namespace 'System.Text'

(0) ShareShare
ReportReport
Posted on by 35
using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Runtime.Serialization;
 using Microsoft.Xrm.Sdk;
 using Microsoft.Xrm.Sdk.Query;
 using System.ServiceModel;

 namespace Patient_registration_number
{

public class Patient_registration_number : IPlugin
{
   
    public void Execute(IServiceProvider serviceProvider)
    {
        try
        {
            IPluginExecutionContext context = 
                 
      (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            if (context.MessageName == "Create")
            {
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] 
              is Entity)
                {
                    IOrganizationServiceFactory serviceFactory = 
      (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = 
     (IOrganizationService)serviceFactory.CreateOrganizationService(context.UserId);
                    Entity accountentity = context.InputParameters["Target"] as Entity;

                    if (accountentity.LogicalName == "account")
                    {
                        Entity Update_current_number = new 
              Entity("preetham_patient_auto_number_registration");
                        
                        StringBuilder autonumber = new StringBuilder();
                        DateTime date = DateTime.Now;
                        string prefix;
                        string suffix;
                        string separator;
                        string current;
                        string year;
                        string month;
                        string day;
                        day = date.Day.ToString("00");
                        year = date.Year.ToString();
                        month = date.Month.ToString("00");
                        
                        QueryExpression query = new QueryExpression()
                        {
                            EntityName = "preetham_patient_auto_number_registration",
                            ColumnSet = new ColumnSet("preetham_name", "preetham_prefix", 
        "preetham_separator", "preetham_sufix", "preetham_currentnumber")
                        };

                        EntityCollection patient_register_number = service.RetrieveMultiple(query);
                        if (patient_register_number.Entities.Count == 0)
                        {
                            return;
                        }
                        foreach (Entity number_format in patient_register_number.Entities)
                        {
                            if (number_format.Attributes.Contains("preetham_name"))
                            {
                                prefix = number_format.GetAttributeValue<string>("preetham_prefix");
                                suffix = number_format.GetAttributeValue<string>("preetham_sufix");
                                separator = number_format.GetAttributeValue<string>("preetham_separator");
                                current = number_format.GetAttributeValue<string>("preetham_currentnumber");
                                int temcurrent = int.Parse(current);
                                temcurrent++;
                                current = temcurrent.ToString("000000");
                                Update_current_number.Id = number_format.Id;
                                Update_current_number.Attributes["preetham_currentnumber"] = temcurrent.ToString();
                                service.Update(Update_current_number);
                                autonumber.Append(prefix + separator + date + month + year + separator + suffix + separator + current);
                                accountentity.Attributes["preetham_registrationnumber"] = autonumber;
                                


                            }
                        }

                    }
                }
            }


        }
        catch (Exception ex)
        {
            ITracingService tracingService = 
        (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            tracingService.Trace("Exception", ex);
        }
       }
     }
    }





contains data from a type that maps to the name 'System.Text:StringBuilder'. The deserializer has no knowledge of any type that maps to this name. Consider changing the implementation of the ResolveName method on your DataContractResolver to return a non-null value for name 'StringBuilder' and namespace 'System.Text'.
I have the same question (0)
  • Eduteam srl Profile Picture
    35 on at

    I am facing the error I’m pasting the code for plugin 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Runtime.Serialization;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    using System.ServiceModel;

    namespace Patient_registration_number
    {
    [DataContract]
    public class Patient_registration_number : IPlugin
    {

    public void Execute(IServiceProvider serviceProvider)
    {
    try
    {
    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

    if (context.MessageName == "Create")
    {
    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
    {
    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    IOrganizationService service = (IOrganizationService)serviceFactory.CreateOrganizationService(context.UserId);
    Entity accountentity = context.InputParameters["Target"] as Entity;

    if (accountentity.LogicalName == "account")
    {
    Entity Update_current_number = new Entity("preetham_patient_auto_number_registration");

    StringBuilder autonumber = new StringBuilder();
    DateTime date = DateTime.Now;
    string prefix;
    string suffix;
    string separator;
    string current;
    string year;
    string month;
    string day;
    day = date.Day.ToString("00");
    year = date.Year.ToString();
    month = date.Month.ToString("00");

    QueryExpression query = new QueryExpression()
    {
    EntityName = "preetham_patient_auto_number_registration",
    ColumnSet = new ColumnSet("preetham_name", "preetham_prefix", "preetham_separator", "preetham_sufix", "preetham_currentnumber")
    };

    EntityCollection patient_register_number = service.RetrieveMultiple(query);
    if (patient_register_number.Entities.Count == 0)
    {
    return;
    }
    foreach (Entity number_format in patient_register_number.Entities)
    {
    if (number_format.Attributes.Contains("preetham_name"))
    {
    prefix = number_format.GetAttributeValue<string>("preetham_prefix");
    suffix = number_format.GetAttributeValue<string>("preetham_sufix");
    separator = number_format.GetAttributeValue<string>("preetham_separator");
    current = number_format.GetAttributeValue<string>("preetham_currentnumber");
    int temcurrent = int.Parse(current);
    temcurrent++;
    current = temcurrent.ToString("000000");
    Update_current_number.Id = number_format.Id;
    Update_current_number.Attributes["preetham_currentnumber"] = temcurrent.ToString();
    service.Update(Update_current_number);
    autonumber.Append(prefix + separator + date + month + year + separator + suffix + separator + current);
    accountentity.Attributes["preetham_registrationnumber"] = autonumber;


    }
    }

    }
    }
    }


    }
    catch (Exception ex)
    {
    ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    tracingService.Trace("Exception", ex);
    }
    }
    }
    }

    can anyone help me to resolve 

  • Suggested answer
    Henry J. Profile Picture
    5,237 on at

    As it's a plugin issue this doesn't really apply to Unified Interface.
    That being said, I advise that you open a support request to get help troubleshooting this issue: https://admin.powerplatform.microsoft.com/support

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Try to update the line

    accountentity.Attributes["preetham_registrationnumber"] = autonumber;

    to line

    accountentity.Attributes["preetham_registrationnumber"] = autonumber.ToString();

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Please avoid opening duplicated threads in different forums. I will merge threads.

  • Eduteam srl Profile Picture
    35 on at

    Thanks and sorry for my mistake

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 171 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 83

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans