Click here to Skip to main content
15,882,209 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a tabel with different kind of phones, loaded from a MSSQL database (Ex: samsung,apple,htc,lg,nokia)...where the user have the possibility to filter this entries with multiple checkboxes. If the user click un "Samsung" the tabel will show only the phones from samsung, if the user click on "iphone" will show only the iphones.
Everything is ok if the user select only one checkbox, my problem is ...if the user select multiple checkboxes, for example show all phones from "Samsung" AND "Apple" the code is not working anymore and nothing show up in the table.
Posted
Comments
ZurdoDev 18-Mar-14 7:12am    
You just need to structure your sql statement properly. Where are you stuck?
Tejas Vaishnav 18-Mar-14 7:22am    
Can you please share your sql query which works fine with single selection...
ErBhati 18-Mar-14 7:39am    
pls paste ur code here

My idea is to compose sql statement dynamically (specifically the where part) based on the checked values.
1. First, you have to loop through the checkboxes to get the checked values, refer: how-to-retrieve-multiple-selected-value-from-aspcheckbox-net-c-sharp[^]
2. Next, in the for loop of above, compose the sql statement by appending the checked value to a new sqlparameter. Refer: how-to-handle-dynamic-sql-parameters[^]
Read more: sqlparametercollection.addwithvalue%28v=vs.110%29.aspx[^]
Adapt this and you should get your solution.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
I have added the following code to help you further, I wrote this code solely in my mind, no debugging, you have to try it out yourself. Good luck.
// Dynamically creating sql query and sql parameters
// according to checked values of checkboxes 
List<SqlParameter> sqlParameters = new List<SqlParameter>();
StringBuilder commandText = new StringBuilder();
commandText.Append("SELECT * FROM table1 WHERE phonetype IN (");
for (int i=0; i < checkboxlist1.Items.Count; i++)
{
    if (checkboxlist1.Items[i].Selected)
    {
         string para = "@type" + i;
         commandText.Append(para);
         commandText.Append(",");
         sqlParameters.Add(new SqlParameter(para, checkboxlist1.Items[i].Text));
    }
}
// remove last commas
commandText--;
commandText.Append(")");

// Bind the sql query and sql parameters and execute
using (SqlConnection connection = new SqlConnection(connectionString))
{
     SqlCommand command = new SqlCommand(commandText.ToString(), connection);
     if (sqlParameters.Count != 0) 
     {
         command.Parameters.AddRange(sqlParameters.ToArray());
     }
 
     SqlDataReader reader = command.ExecuteReader();
     while (reader.Read())
     {
	 string name = reader.GetString(1);
	 string price = reader.GetString(2);
	 // other code...
     }
}
 
Share this answer
 
v6
Comments
Member 10194425 18-Mar-14 8:21am    
how to pass multiple selected values to database?
Peter Leow 18-Mar-14 11:10am    
Added sample code. Good luck.
use or instead of and then it will work
 
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