listbox items looping problem

Discussion in 'ASP .Net' started by nasirmajor@yahoo.com, Jun 19, 2006.

  1. Guest

    dear all,
    i have a listbox whose items are displayed in listbox e.g
    ('1','3','4',)
    my problem is the last comma (, after 4) i don't want this comma as i
    have to use
    this string in sql query.
    the loop through procedure is this

    protected void Button1_Click1(object sender, EventArgs e)
    {
    string msg = "";

    foreach (ListItem li in listboxcategory.Items)
    {
    if (li.Selected == true)
    {
    msg += "'" + li.Value + "'" + ",";
    }
    }
    Label1.Text = "("+ msg + ")";
    }

    thanks in advance
     
    , Jun 19, 2006
    #1
    1. Advertising

  2. Ray Booysen Guest

    wrote:
    > dear all,
    > i have a listbox whose items are displayed in listbox e.g
    > ('1','3','4',)
    > my problem is the last comma (, after 4) i don't want this comma as i
    > have to use
    > this string in sql query.
    > the loop through procedure is this
    >
    > protected void Button1_Click1(object sender, EventArgs e)
    > {
    > string msg = "";
    >
    > foreach (ListItem li in listboxcategory.Items)
    > {
    > if (li.Selected == true)
    > {
    > msg += "'" + li.Value + "'" + ",";
    > }
    > }
    > Label1.Text = "("+ msg + ")";
    > }
    >
    > thanks in advance
    >

    You can use substring on the variable msg after the loop to remove the
    trailing comma.
     
    Ray Booysen, Jun 19, 2006
    #2
    1. Advertising

  3. Guest

    Ray Booysen wrote:
    > wrote:
    > > dear all,
    > > i have a listbox whose items are displayed in listbox e.g
    > > ('1','3','4',)
    > > my problem is the last comma (, after 4) i don't want this comma as i
    > > have to use
    > > this string in sql query.
    > > the loop through procedure is this
    > >
    > > protected void Button1_Click1(object sender, EventArgs e)
    > > {
    > > string msg = "";
    > >
    > > foreach (ListItem li in listboxcategory.Items)
    > > {
    > > if (li.Selected == true)
    > > {
    > > msg += "'" + li.Value + "'" + ",";
    > > }
    > > }
    > > Label1.Text = "("+ msg + ")";
    > > }
    > >
    > > thanks in advance
    > >

    > You can use substring on the variable msg after the loop to remove the
    > trailing comma.


    dear please give any code example using c#
     
    , Jun 19, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kent
    Replies:
    1
    Views:
    883
    Andrew de la Harpe
    May 3, 2004
  2. Bart Van Hemelen
    Replies:
    2
    Views:
    4,679
    Bart Van Hemelen
    Sep 4, 2006
  3. K B
    Replies:
    2
    Views:
    670
  4. Tomas
    Replies:
    1
    Views:
    575
    Munna
    Aug 12, 2008
  5. Replies:
    0
    Views:
    121
Loading...

Share This Page