Sunday, October 11, 2009

Dealing with Carriage Returns in Web Service Methods

When returning data from a database through a Web Service (as a DataSet, or as an XML representation of the data in the DataSet), you may have issues with new lines (carriage return/line feed) in your string data. 

I use a SQL Server database. The "new line" data must be stored in the database like this: "\r\n". But, when passing this through a web service, it must be passed like this: "&#13". Consequently, to address this discrepancy in format, I use two methods in my Web Service base class: a ConvertCarriageReturnForGets() method and a ConvertCarriageReturnForSaves() method.

[WebService(Namespace = "http://mycompany.com/")]
public class BBWebServices : System.Web.Services.WebService
{
#region Declarations

private string CRLF = "
";
private string NewLine = "\r\n";

#endregion

#region Convert CarriageReturn Methods

/// <summary>
/// Converts CarriageReturn/LineFeed characters so they are passed correctly through the Web Service
/// </summary>
/// <param name="dt"></param>
/// <param name="ColumnName"></param>
protected void ConvertCarriageReturnForGets(DataTable dt, string ColumnName)
{
if (dt.Columns.Contains(ColumnName))
{
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i][ColumnName] != DBNull.Value)
dt.Rows[i][ColumnName] = dt.Rows[i][ColumnName].ToString().Replace(this.NewLine, this.CRLF);
}
}
}
/// <summary>
/// Converts CarriageReturn/LineFeed characters so they are correctly saved to the database
/// </summary>
/// <param name="dt"></param>
/// <param name="ColumnName"></param>
protected void ConvertCarriageReturnForSaves(DataTable dt, string ColumnName)
{
if (dt.Columns.Contains(ColumnName))
{
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i][ColumnName] != DBNull.Value)
dt.Rows[i][ColumnName] = dt.Rows[i][ColumnName].ToString().Replace(this.CRLF, this.NewLine);
}
}
}

#endregion
}

Use the methods like this in your Web Service classes:

[WebService(Namespace = "http://mycompany.com/")]
public class Customer : BBWebServices
{
[WebMethod(Description="Returns Customer Info for one customer")]
public string GetCustomer(long CustomerKey)
{
CustomerBiz oBiz = new CustomerBiz();
CustomerDataSet ds = oBiz.GetCustomer(CustomerKey);

this.ConvertCarriageReturnForGets(ds.CustomerNotes, "notes");

string XML = ds.GetXml();
return XML;
}
[WebMethod(Description="Saves info for one customer")]
public bool SaveCustomer(string ChangeXML, string DeleteXML)
{
CustomerDataSet dsChanged = new CustomerDataSet();
CustomerDataSet dsDeleted = new CustomerDataSet();

try
{
// FillWithXml() is a method I have defined in my DataSet classes
// It simply uses the DataSet's ReadXml() method with a string reader
dsChanged.FillWithXml(ChangeXML);
if (DeleteXML != "")
dsDeleted.FillWithXml(DeleteXML);
}
catch (Exception ex)
{
Message = ex.Message;
return false;
}

CustomerBiz oBiz = new CustomerBiz();
this.ConvertCarriageReturnForSaves(dsChanged.CustomerNotes, "notes");

bool IsOK = o.SaveCustomer(dsChanged, dsDeleted);
return IsOK;
}
}

2 comments:

  1. Trying implement in this VB. You may have saved my project!!!

    Thanks

    ReplyDelete
    Replies
    1. You're welcome! I'm glad it helped you!

      Delete