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.

2
  • You can use string.join. But you should worry about SQL injection Commented May 12, 2014 at 13:24
  • @Uriil can u explain. Currently i just need to do it for my exam tommorow so its ok i will manage. Commented May 12, 2014 at 13:26

2 Answers 2

1

Just replace this line

queryt += ")";

with

queryt=queryt.TrimEnd(',') +")";
Sign up to request clarification or add additional context in comments.

3 Comments

Its not write. Output now is And Brand IN('BlackBerry','Micromax',
I tested it. It works fine. Did u replace the line - > queryt += ")"; ?
Yes its working. Actually intellicence selected another var query rather than queryt. Thanks.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.