Click here to Skip to main content
15,913,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have four dropdownlist and i want to search on their selected item on button click. what to write in where condition of query string ??
the condition i want is :
1. if either one of the four is selected and other three are not selected search happens based on that 1 selected items
2. if either two of the four is selected and other two are not selected search happens based on that 2 selected items
3. if either three of the four is selected and other one is not selected search happens based on that 3 selected items
4. if all of the four is selected search happens based on that 4 selected items.


Will appreciate your help!
Posted
Updated 7-Apr-14 21:25pm
v2

Use Below code in the button click event

C#
try
       {

           DataSet Ds = new DataSet();
           string category = "";

           if (DropDownList1.SelectedIndex > 0)
           {
               category = "cat = '" + DropDownList1.SelectedItem.Text + "' AND";

           }
           else
           {
               category = "";
           }

           string sql = "";

           if (DropDownList2.SelectedIndex > 0)
           {
               sql = sql + "subcat='" + DropDownList2.SelectedItem.Text + "' and ";
           }
           if (DropDownList3.SelectedIndex > 0)
           {
               sql = sql + "type='" + DropDownList3.SelectedItem.Text + "' and ";
           }
           
           if (DropDownList4.SelectedIndex > 0)
           {
               sql = sql + "cat='" + DropDownList4.SelectedItem.Text + "' and ";
           }
           sql = "SELECT * FROM  tb_products where " + category + "  " + sql + "STATUS=1";
           Ds = Rajprasad.ExcuteSqlwithsql(sql);

               DataList1.DataSource = Ds;
               DataList1.DataBind();

       }
       catch (Exception ex)
       {


       }
 
Share this answer
 
Comments
jayraj86 8-Apr-14 4:16am    
This is my on button click code what changes should i make in this code ??

SqlConnection con = new SqlConnection("Data Source=jayraj-pc\\sqlexpress;Initial Catalog=Internship;Integrated Security=True;Pooling=False");
con.Open();
//if (Degree2.SelectedIndex > 0 && branch2.SelectedIndex > 0 && DropDownList1.SelectedIndex > 0 && DropDownList2.SelectedIndex > 0)
//{
string str = "select B.CompanyLogo as ' ',A.Title,A.InternshipStartDate,A.ApplicationDeadline,A.Duration,A.InternshipCity,A.Category,A.Stipend,A.InternshipID,A.Degree,A.Branch from Internship AS A INNER JOIN Companies AS B ON A.CompanyID=B.CompanyID where (Degree='" + Degree2.SelectedItem.Text + "' and Branch='" + branch2.SelectedItem.Text + "') or InternshipCity ='" + DropDownList1.SelectedItem.Text + "' or Category='" + DropDownList2.SelectedItem.Text + "'";
Response.Write(str);
SqlCommand cmd = new SqlCommand(str, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlDataReader dr = cmd.ExecuteReader();



GridView2.DataSource = dr;
GridView2.DataBind();

//Response.Write(GridView2.Columns.Count.ToString());
con.Close();
use store procedure as below


XML
create procedure usp_test
ddl1 int = 0  ,
ddl2 int = 0  ,
ddl3 int = 0  ,
ddl4 int = 0
as
begin

select * from <tablename>
where <field1> = case ddl1 when 0 then <field1> else ddl1 end   and
<field2> = case ddl2 when 0 then <field2> else ddl2 end and
<field3> = case ddl3 when 0 then <field3> else ddl3 end and
<field4> = case ddl4 when 0 then <field4> else ddl4 end and

end
 
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