Saturday, July 17, 2010

Unable to switch the encoding at System.Data.SqlClient.SqlConnection

Symptom:

You have done the following:
  • You have set up an SQL table with XML data type in it.
  • You have a Strongly Typed DataSet
  • You try to write XML from a C# String
  • You get the following error
You have a DataSet unable to switch the encoding at System.Data.SqlClient.SqlConnection

What Fixed It For Me

The part of the original XML which went into the string had


in the XML heading

by simply switching this to


That fixed the problem.

Note:
  • In my case the real issue was that the actual strung was encoded in Unicode (UTF-16) but the XML header said utf-8. That mismatched.
  • In your case the actual string may be encoded in some other format such as utf-8. Be sure to check. If the real encoding is the issue then you could re-encode your string from what you have into UTF-16. Look for System.Text.Encoding class on MSDN for further information.


Post a Comment