3
Answers

Filter using C# and asp.net

Manali Patil

Manali Patil

9y
612
1
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();
}
}
}
}
}


}
Answers (3)
0
Manoj Bhoir

Manoj Bhoir

NA 7.6k 294.2k 9y
Hi,

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


Accepted
0
Manali Patil

Manali Patil

NA 6 615 9y
THANK YOU!!

But I also want to know how to filter data according to the selection of multiple checkboxes in multiple checkboxlists
0
Dipankar Biswas

Dipankar Biswas

NA 3k 125.6k 9y
hi...
check your connection ....

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings
["testConnectionString14"].ConnectionString);

cm.Connection.Open();
 here not clear...