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 BhoirPosted May 8, 2015, 12:51 PM
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
Manali PatilPosted May 8, 2015, 1:45 PM
But I also want to know how to filter data according to the selection of multiple checkboxes in multiple checkboxlists
Dipankar BiswasPosted May 8, 2015, 11:30 AM
check your connection ....
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings
["testConnectionString14"].ConnectionString);
cm.Connection.Open();
here not clear...