user1591985
user1591985

Reputation:

Split() function isn't giving output

Whats wrong in the code..? It isn't working!!

Actually I want to split the entries from one of the field of database.. The items in it are separated by commas..

Here is what I am doing.

string str = dataSet.Tables[0].Rows[0]["Ingredients"].ToString();

string[] split = str.Split(',');
IList<string> lblListItemIngredients  = new List<string>();
foreach (string item in split)
{
  lblListItemIngredients.Add(item);
}

and in my aspx page,

<ul>
<li>
<asp:label id="lblListItemIngredients" runat="server></asp:Label>
</li>
</ul>

But the output isn't coming, but in debugging mode, i can see the string is splitting.. Whats wrong?

Upvotes: 1

Views: 185

Answers (4)

Ian G
Ian G

Reputation: 30234

Guffa is bang-on already, but just for completeness, and because it is in keeping with the spirit of how you are were doing it in the first place :-)

if you updated your mark up to:

   <ul>
   <asp:Literal id="literalIngredients" runat="server" />
   </ul>

(To be honest your original code would have more or less worked if inside your foreach loop you used yourLabel.Text += item;, as Guffa said, it is not really a good idea to call the variable and the control the same name)

and updated your code to

    string str = dataSet.Tables[0].Rows[0]["Ingredients"].ToString();

    string[] split = str.Split(',');
    // not needed IList<string> lblListItemIngredients  = new List<string>();
    foreach (string item in split)
    {
      literalIngredients.Text += string.Format("<li>{0}</li>",item);
    }

Upvotes: 1

David W
David W

Reputation: 10184

You are using an asp:label control in your page markup when you should be using something like an asp:ListBox. Eliminate the IList<String> declaration from your code as well.

A better way to add an array of strings to a ListBox would merely be as follows:

First, in your markup, change the improper control reference to a ListBox:

    <asp:ListBox id="IngredientList" runat="server"></asp:ListBox>

Secondly, in the source, streamline the addition:

    string str = dataSet.Tables[0].Rows[0]["Ingredients"].ToString();

    string[] ingredients= str.Split(',');
    IngredientList.Items.AddRange(ingredients);

This method eliminates the incorrect label control in the markkup, and from the code behind eliminates the manual iteration and the unneeded IList declaration in the original version. Hope this helps.

Upvotes: 1

Guffa
Guffa

Reputation: 700302

You have to get the data in the list to the control somehow. That doesn't happen magically just because you give a variable the same name as the id of the control.

Actually, you should use a different name for the variable, otherwise it will shadow the property that has been added to the page object.

If you want to make a HTML list it's not enough to put a label in one list item, that won't create one list item for each string. You can use a repeater:

<ul>
  <asp:Repeater id="lblListItemIngredients" runat="server">
    <ItemTemplate>
      <li><%# Container.DataItem %></li>
    </ItemTemplate>
  </asp:Repeater>
</ul>

You don't have to create a list for a data source, an array works just fine:

string str = dataSet.Tables[0].Rows[0]["Ingredients"].ToString();
string[] split = str.Split(',');

lblListItemIngredients.DataSource = split;
lblListItemIngredients.DataBind();

Upvotes: 3

Ronak Shah
Ronak Shah

Reputation: 35

You Should be using asp:ListBox instead of Label

Upvotes: 0

Related Questions