Whitespace handling in web services has changed between 1.1 and 2.0. If a
field in a dataset contains only spaces, 1.1 returned an empty string when it
crossed the web services boundary. Under 2.0, it preserves the spaces.
While this is correct, it breaks some of our code that was working. Any easy
work-around?
The following web service/client shows the problem. Try it in 1.1 and 2.0.
*** Service ***
[WebMethod]
public DataSet GiveMeWhitespace()
{
DataSet testDS = new DataSet();
DataTable testTbl = new DataTable("MyTable");
testDS.Tables.Add(testTbl);
DataColumn testCol = new DataColumn("Col1", "abc".GetType());
testCol.AllowDBNull = true;
testTbl.Columns.Add(testCol);
DataRow newRow = testTbl.NewRow();
testTbl.Rows.Add(newRow);
newRow["Col1"] = null;
newRow = testTbl.NewRow();
testTbl.Rows.Add(newRow);
newRow["Col1"] = "";
newRow = testTbl.NewRow();
testTbl.Rows.Add(newRow);
newRow["Col1"] = " ";
newRow = testTbl.NewRow();
testTbl.Rows.Add(newRow);
newRow["Col1"] = " ";
newRow = testTbl.NewRow();
testTbl.Rows.Add(newRow);
newRow["Col1"] = "A value";
newRow = testTbl.NewRow();
testTbl.Rows.Add(newRow);
newRow["Col1"] = "Another value";
newRow = testTbl.NewRow();
testTbl.Rows.Add(newRow);
newRow["Col1"] = "A value ";
newRow = testTbl.NewRow();
testTbl.Rows.Add(newRow);
newRow["Col1"] = "Another value ";
return testDS;
}
*** Client ***
localhost.Service1 svc = new localhost.Service1();
DataSet testDS = svc.GiveMeWhitespace();
foreach (DataRow aRow in testDS.Tables[0].Rows)
{
if (!aRow.IsNull("Col1"))
{
TextBox1.Text = TextBox1.Text + "*" + aRow["Col1"].ToString() + "*\n";
}
else
{
TextBox1.Text = TextBox1.Text + "*{null}*\n";
}
}
> Whitespace handling in web services has changed between 1.1 and 2.0. If a
> field in a dataset contains only spaces, 1.1 returned an empty string when
[quoted text clipped - 3 lines]
> easy
> work-around?
Hi Frobozzj,
I am not aware of this change, but makes sense to me to return the exact
number of spaces as they were in the original data. You can apply
String.Trim() to your data to get the same behavior as before.
Regards,

Signature
Martin Kulov
http://www.codeattest.com/blogs/martin
MCT
MCSD.NET Early Achiever
Frobozz - 17 Mar 2006 00:25 GMT
I agree that 2.0 is handling it correctly, and I understand the I can do a
Trim(). The problem is that this could affect much of my existing code, and
is quite hard to search for.
I'm scanning my code for == "", == String.Empty, .Compare(""), and the !=
cases as well. It could be that there is only one place (the one I stumbled
across) that needs to be changed, but it's difficult to tell.
This is one of the biggest "gotcha's" I've run across in upgrading to
VS2005, and very few (if any) people are talking about it on the web.
> I am not aware of this change, but makes sense to me to return the exact
> number of spaces as they were in the original data. You can apply
> String.Trim() to your data to get the same behavior as before.