listbox items looping problem

N

nasirmajor

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
 
R

Ray Booysen

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top