Click here to Skip to main content
16,012,223 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working for an online shopping website. So for searching the products I need to show the search results according to checking the check box lists and show the results in a data set. So please help me.. Thanks in advance....
Posted
Updated 2-May-13 0:32am
v3
Comments
Thanks7872 2-May-13 3:46am    
Have you tried something?if you search for the same phrase in google it will give you plenty of results.Try looking at this link.
Member 9762839 2-May-13 7:58am    
yaaa i tried...

Source page
---------------

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource1" DataTextField="NationalityID"
DataValueField="NationalityID"
onselectedindexchanged="CheckBoxList1_SelectedIndexChanged">

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:UllasConnectionString %>"
SelectCommand="SELECT [NationalityID] FROM [Nationality]">

<br />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<columns>
<asp:BoundField DataField="NationalityID" HeaderText="NationalityID"
SortExpression="NationalityID" />
<asp:BoundField DataField="Nationality" HeaderText="Nationality"
SortExpression="Nationality" />



</div>
</form>
</body>
</html>

Code behind
--------------

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

}
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{

SqlConnection cnn = new SqlConnection(
ConfigurationManager.ConnectionStrings["UllasConnectionString"].ConnectionString.Trim());

SqlCommand cmd = new SqlCommand();

String sqlText = "select * from Nationality ";
String sqlFilterText = "";
int index = 0;
foreach (ListItem item in CheckBoxList1.Items)
{
index += 1;
if (item.Selected)
{
String paramName = "@param" + index.ToString().Trim();
SqlParameter param = new SqlParameter(paramName, SqlDbType.UniqueIdentifier);

//param.Value = new Guid(item.Value.Trim());

param.Value =new Guid (item.Value.Trim());


cmd.Parameters.Add(param);

sqlFilterText += " NationalityID = " + paramName + " or ";
}
}

if (!String.IsNullOrEmpty(sqlFilterText))
{
sqlText += " Where " + sqlFilterText.Substring(0, sqlFilterText.Length - 3);
}
cmd.CommandText = sqlText;
cmd.Connection = cnn;

SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable tbl = new DataTable();
adapter.Fill(tbl);
GridView1.DataSource = tbl;
GridView1.DataBind();

}
}


But it showing the following error

Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).

1 solution

C#
private string GetSql()
{
    bool a = true;
    bool b = true;

    System.Text.StringBuilder sb = new System.Text.StringBuilder();

    sb.AppendFormat("SELECT * FROM mytable WHERE 1 = 1");

    if (a)
        sb.AppendFormat(" AND `aa` = 1");
    if (b)
        sb.AppendFormat(" AND `bb` = 1");

    sb.AppendFormat(";");

    return sb.ToString();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900