ASP.NET with C#: Appending Values to stringbuilder

  • ------------

    string strSource;

    foundVendors = appVendTable.Select("Item = '" + [foundRows.Length-1]["COMPONENT"].ToString()

    + "'");

    if (foundVendors.Length == 0)

    {

         strSource = "None";

        //rangeSOURCE.Value2 = "None";

        //outputStr.Append("<TD>=\""+"None"+ "\"</TD>");

    }

    if (foundVendors.Length == 1)

    {

         strSource = "Single";

        //rangeSOURCE.Value2 = "Single";

       //outputStr.Append("<TD>=\""+"Single"+ "\"</TD>");

    }

    if (foundVendors.Length > 1)

    {

         strSource = "Multiple";

       //rangeSOURCE.Value2 = "Multiple";

       //outputStr.Append("<TD>=\""+"Multiple"+ "\"</TD>");

    }

    ------------

    I am delclaring a string variable like "string strSource;" before using it in the

    if-conditions.Then at the end I want to iterate through the dataset and then append it using stringbuilder.

    ----------

    foreach (DataRow pRow in mlBomDS.Tables["ITEM_MULTILEVELBILL"].Rows)

    {

    outputStr.Append("<TR>");

      

    outputStr.Append("<TD>" + strSource + "</TD>");

    outputStr.Append("<TD>=\""+foundItems[0]["LOT_SZ_MUL"].ToString()+ "\"</TD>");

    outputStr.Append("</TR>");

    }

    -----------

    So my problem is, it only Appends the "strSource" instead of the value based on the

    if-Condition.

    "FoundVendors" and "FoundItems" are DataRow type.

    My another Problem is when I append

    outputStr.Append("<TD>=\""+foundItems[0]["LOT_SZ_MUL"].ToString()+ "\"</TD>");

    It is only able to append one single value for all rows.

    If I try to intialize "int i" and then place

    outputStr.Append("<TD>=\""+foundItems["LOT_SZ_MUL"].ToString()+ "\"</TD>");

    Then it complains that the "array is out of bound."

    How do I get it to append different values for different rows of the datatable.

     

    Thanks for your help in advance!

  • This was removed by the editor as SPAM

  • First, strSource is a String, not a StringBuilder, so it is immutable.  You'd need:

    strSource = strSource.Append( "something" );

    But you should certianly use a StringBuilder for work like this.

     

     

  • Thanks Robert.

    Yes I understand what you say.I tried many ways but did not get the output the way I wanted.

    At last I could find a way to append the string values using stringbuilder without messing up with rows in data table.Previously I was using stringbuilder to append the values of string while looping for each row in data table,which did not help.Now I got the output the way I wanted.

    Thanks for your time and suggestion,

    Alice.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply