Manali Patil

Manali Patil

  • NA
  • 137
  • 1.5k

Filter using C# and asp.net

May 8 2015 11:20 AM
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)