5
Answers

How to write sql query using checkedlistbox?

Hi,

I am going to develop the form like below screen shot. I wrote the code in itemCheck event to insert the checked item text in textbox.



My problem is: if checked items are greater than 2 I want to insert "," (comma) in between the two items.

by selecting the items from checked list box I have to write SQL query.

by removing the selection from checked list box we have to remove that item text from the query in textbox.



Can any one plz help me with code.

Thanks
Murali krishna
Answers (5)
0
Praneeth Kumar

Praneeth Kumar

NA 832 43.4k 13y
I assume you are putting checked items inside an array or list. so your code should be like below
string query;
if(checkedcount==1)
{

query="select "+checkedvalue[0]+" from tablename";
}
else
{
StringBuilder columns=new StringBuilder();
        for(int i=0;i<checkedValues.Length;i++)
        {
                if(i<checkedValues.Length-1)
                {
                        columns.Append(checkedValues[i]);
                        columns.Append(",");
                
                }
                else
                {
                    // No more commas needed as this is last column
                        columns.Append(checkedValue[i]);
                }

          }

query="select "+columns+" from tablename";
}

execute ur query.

I hope this query helps..This is just a rough code snippet.
Accepted
0
Suthish Nair

Suthish Nair

NA 38.4k 4.6m 13y
So request you accept all three methods as Answers. This will helpful for others also.
0
Murali krishna  Ande

Murali krishna Ande

NA 43 39.2k 13y
Thanks for your valuable suggestions guys....

Problem solved..

Once again thanks
Murali Krishna

0
Suthish Nair

Suthish Nair

NA 38.4k 4.6m 13y

 try...

 

 -- To get all checked items from Checkboxlist using LINQ
 using System.Collections.Generic;
 using System.Linq;

 IEnumerable<string> allCheckedValues = chboxlist.Items
.Cast<ListItem>()
.Where(item => item.Selected)
.Select(item => item.Value);

 String ss = "";
 foreach (var item in allChecked)
 {
   ss = (ss == "" ? "[" + item + "]" : ss + ", [" + item + "]");
 }

 String query1 = String.Format("Select {0} from {1}", ss, "Table1");

 If not want to use LINQ, then use for each to loop the checkbox list.

0
Muralidharan Deenathayalan

Muralidharan Deenathayalan

NA 21.9k 1.5m 13y


 

  private void btn_Click(object sender, EventArgs e)
        {
             string s = "select ";

            for (int i=0;i<checkedListBox1.CheckedItems.Count;i++)
            {
                 if (checkedListBox1.CheckedItems.Count > i)
                     if (checkedListBox1.CheckedItems.Count-1 == i)
                     {
                         s += checkedListBox1.Items[i].ToString() ;
                     }
                     else
                     {
                         s += checkedListBox1.Items[i].ToString() + " , ";
                     }
            }

            label1.Text = s;
        }



Try this.