how to code where there are multiple checkboxlists, when I select a particular value/values from a single or multiple checkboxlists, then the corresponding data to the selected value/values should be displayed in my DATALIST.
Its similar to the filteration done on Online Shopping sites.
Output:
only one checkbox selected data is display..multiple selection is not working.
ERROR: The ConnectionString property has not been initialized.
THANK YOU.
CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
namespace Filtering
{
public partial class DATALIST : System.Web.UI.Page
{
//MySQLHelper.ExecuteNonQuery(ConfigurationManager.ConnectionStrings["testConnectionString14"].ConnectionString,CommandType.Text,sqlQuery,sqlParams);
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString14"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//CheckBoxList1.SelectedIndex = 1;
}
}
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
string sCheckedValue = "";
foreach (ListItem oItem in CheckBoxList1.Items)
{
if (oItem.Selected)
{
if (sCheckedValue == "")
sCheckedValue = "Selected Value : " + oItem.Value + " Selected Text: " + oItem.Text;
// Response.Write(sCheckedValue);
try
{
SqlCommand cm = new SqlCommand("", con);
string param = oItem.Value;
Response.Write(param + "
");
cm.CommandText = "SELECT * FROM [VENUE] WHERE [location] IN (@param)";
//Response.Write(cm.CommandText.ToString());
cm.Parameters.Add("@param", SqlDbType.VarChar).Value = param;
cm.Connection.Open();
/*foreach (SqlParameter a in cm.Parameters)
{
Response.Write(a.ToString());
}*/
DataList1.DataSource = cm.ExecuteReader();
DataList1.DataBind();
cm.Connection.Close();
cm.Connection.Dispose();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
con.Close();
}
}
}
}
}
}
Loading

Manoj BhoirRank #599 · 4K reputationPosted May 8, 2015, 8:51 AM EDT
I think cm.Connection.Dispose(); is your Problem.
When first time page load occures it will initialize your connection and when you click checkbox you will get your data but because of Dispose your connection object gets disposed and your are not initializing it again so it thorw Error.
So either remove
cm.Connection.Dispose();
Or add con = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString14"].ConnectionString);
in CheckBoxList1_SelectedIndexChanged
Dipankar BiswasRank #992 · 2K reputationPosted May 8, 2015, 7:30 AM EDT
check your connection ....
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings
["testConnectionString14"].ConnectionString);
cm.Connection.Open();
here not clear...
Manali Patil124 reputationPosted May 8, 2015, 9:45 AM EDT
But I also want to know how to filter data according to the selection of multiple checkboxes in multiple checkboxlists