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();

   // 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 = 
         + "|" + territory.TerritoryName + "@@");

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


   Cursor.Current = Cursors.Default;
comments powered by Disqus
Upcoming Events

.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.