Thursday 11 September 2008

Error messages "There is an error in XML document (1, 1994)." or inner exception of "DataTable does not match to any DataTable in source."

Today I have spent nearly 5 hours trying to get to the bottom of why I was receiving these errors.

The first error made me think that this was caused by an invalid character in my data, which was being returned from a webservice to my data in my program.

I took a copy of what was being returned using "fiddler" to see what was going on in my browser - it showed the xml being returned was fine. I even validated the XML to check.

The interesting thing was the inner exception of "DataTable does not match to any DataTable in source." thrown in Visual Studio. Looking at this, it was telling me that the datatable being returned from the webservice, was not the same as the datatable I had in Visual Studio. The strange this was, they were both typed datatables! so how could they be wrong.... well, they weren't.

The source of the problem was the following which was at position 1994 in the XML:

"diffgr:diffgram msdata="urn:schemas-microsoft-com:xml-msdata" "

This was telling me that there was a difference in the schema of the dataset... <- Note here, a difference in the DATASET. I was not returning a dataset, I was returning a datatable! Anyway, a quick google brought up a nice little link from the good boys at Microsoft:

http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=105642

It turns out, you can't return a datatable from a webservice, you have to return a DATASET. What a load of pants.

So, to solve this, I had to alter the webservice method to return a fake dataset, that contained the datatable that I wanted, e.g:

[WebMethod]
public MyTypedDataSet(string someparams)
{
MyTypedDataSet ds = new MyTypedDataSet();
MyTypedDataTable dt = aFunctionThatGetsTheData(someparams);

ds.Tables.Add(dt.Copy());
return ds;
}
private MyTypedDataSet.MyTypedDataTable(string someinfo)
{
MyTypedDataSet.MyTypedDataTable dt;
//The work that gets the data is here
return dt;
}

I hope this helps someone if they have the same problem too!!!

Jamie.

4 comments:

  1. Thank you for your post! I got the same problem with WCF. And resolved it only with helping of your post!

    ReplyDelete
  2. You can return a DataTable from a web service, you just need to make sure the TableName is set.

    ReplyDelete