Reduce Code and Server Roundtrips: Listing 1: C#: Create a Delimited String on the Client

Creating a delimited string on the client is a fairly trivial task using a StringBuilder object. The code simply loops through the selected territories in the ListBox and builds a delimited string, separating column values with "|" and row values with "@@". The delimited string is then passed to the stored procedure as a VARCHAR parameter. The search results are returned from the stored procedure as a DataSet and bound to the grid on the form.

private void btnGetOrdersDelimited_Click(
   object sender, EventArgs e)
{
   Cursor.Current = Cursors.WaitCursor;

   StringBuilder sb = new StringBuilder();
   ApplicationData.TerritoryDS.TerritoryRow 
   territory;

   // build the delimited string containing 
   // selected territories to pass as a stored 
   // procedure parameter. Columns are 
   // separated by "|" and rows are 
   // separated by "@@"
   for (int i = 0; i <       
   this.lstTerritory.SelectedIndices.Count; i++)
   {
      territory = 
      this._territoryDS.Territory[
      this.lstTerritory.SelectedIndices[i]];
      sb.Append(territory.ID 
         + "|" + territory.TerritoryName + "@@");
   }

   OrdersDS ds = 
      BusinessLayer.BusinessServices.GetOrders(
      sb.ToString(), DemoForm.databaseConnection);

   this.BindGrid(ds);

   Cursor.Current = Cursors.Default;
}
comments powered by Disqus

Featured

Subscribe on YouTube