0

I want to create a sql query based on the selected Item from checkboxlist. Just want to remove last comma from the code below.

    String queryt=" And Brand IN(";
        foreach (ListItem lst in brandcklist.Items)
        {
            if (lst.Selected == true)
            {
               queryt += "'"+lst.Text+"',";
            }
        }
        queryt += ")";
        Label3.Text = queryt;

output for this is
And Brand IN('BlackBerry','Karbonn',)
note the comma after karbonn, I don't want to add comma after last item.

ALOK
  • 553
  • 6
  • 17

2 Answers2

1

Just replace this line

queryt += ")";

with

queryt=queryt.TrimEnd(',') +")";
Ashish Charan
  • 2,347
  • 4
  • 21
  • 33
0

Something like this:

String queryt = string.Format(" And Brand IN ({0})", string.join(", ", brandcklist.Items.Where(p=>p.Selected).Select(p=>p.Text)))

But as i said, it' open for SQL injection. Here is better description, how you should do this

Community
  • 1
  • 1
Uriil
  • 11,948
  • 11
  • 47
  • 68