Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to get comma separated values of checked items values of CheckedListBox and pass on to the Where condition sql query

like
C#
select * from table_name where t_id IN ('8','9')
Posted
Updated 6-Mar-17 4:17am
v4
Comments
Thomas Daniels 28-Dec-13 4:54am    
Can you add more explanation please?
Praveen_P 28-Dec-13 4:57am    
hi ,I updated my question , any solution
Tom Marvolo Riddle 28-Dec-13 4:56am    
Not clear.provide more info
Praveen_P 28-Dec-13 5:02am    
i have a repeater control , i need to load data to it according to selected items of a check box list, i use the following code to load repeater control

foreach (ListItem lst in ChkM.Items)
{
if (lst.Selected)
{
string str = "select * from table_name where t_id='" + ChkM.SelectedValue + "'";
DataTable dt = new DataTable();
dt = con.select(str);
if (dt.Rows.Count >= 1)
{
rptr.DataSource = dt;
rptr.DataBind();
}

}
}

but works only one selected value. Not working multiple selected values.
♥…ЯҠ…♥ 28-Dec-13 5:14am    
You sure, iteration is working fine? if you select 3 checkbox then three times iteration is happening?

Hi Praveen,

You could try this code
C#
string whereCndn = string.Empty;
foreach (var item in ChkM.SelectedItems)
        {
            if (item.Selected)
            {
               whereCndn += item.SelectedValue + ","; //Here you will get set of selected checkbox value like (8,9,)
            }
        }

        string str = "select * from table_name where t_id IN ("+ whereCndn.Remove(whereCndn.Length - 1) +")"; //Removing last comma character from the string and putting it in string
               DataTable dt = new DataTable();
                dt = con.select(str);
                if (dt.Rows.Count >= 1)
                {
                    rptr.DataSource = dt;
                    rptr.DataBind();
                }

Hope this helps you a bit.

Regards,
RK
 
Share this answer
 
v6
Comments
Praveen_P 28-Dec-13 5:41am    
Hi rk
But i got an exception 'Conversion failed when converting the varchar value '8,8' to data type int.' Thanks
♥…ЯҠ…♥ 28-Dec-13 6:02am    
Updated my solution, now try
Praveen_P 28-Dec-13 6:17am    
Hi rk thanks for your solution still getting exception
my query is "select * from table_name where t_id IN ('9,8')" can i make it like
"select * from table_name where t_id IN (9,8)" which avoids exception
♥…ЯҠ…♥ 28-Dec-13 6:19am    
which will do, in your question you have given as such thats why I added ' in my solution.Now it would get resolved.
Tom Marvolo Riddle 28-Dec-13 6:03am    
5!
Store the Checked values in a string variable . The use TrimEnd() method.

var selectedVal = str.TrimEnd(',');


Then use it in the where condition of the sql.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900