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.
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));
}
}
commandText--;
commandText.Append(")");
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);
}
}