Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to Form Where Clause on the Basis of CheckListBox Selected Items
This my sp
SQL
BEGIN
declare @SQL VARCHAR(MAX)
 
SET @SQL='Select [Hadith_Text]
      ,[Hadith_Urdu]
      ,[Hadith_English]
      ,[Chapter_English_Name]
      ,[Chapter_Urdu_Name]
      ,[Baab_English_Name]
      ,[Baab_Urdu_Name]
      ,[Baab_Id]
      ,[Hadith_Book_Number]
	  From Hadiths_old
	  Where ID < 34192 '
 
IF @WhereClause<>''
	SET @SQL=@SQL+'  '+ @WhereClause + ' order by ID'
 
EXEC(@SQL)


while in C#
C#
private void buttonX1_Click(object sender, EventArgs e)
       {

           if (clbBooksName.GetItemChecked(0) == true)
           {
               BookSelection += "OR Hadith_Book_Number = 1 ";
           }
            if (clbBooksName.GetItemChecked(1) == true)
           {
               BookSelection += "OR Hadith_Book_Number = 2 ";
           }
          if (clbBooksName.GetItemChecked(2) == true)
           {
               BookSelection += "OR Hadith_Book_Number = 3 ";
           }
           if (clbBooksName.GetItemChecked(3) == true)
           {
               BookSelection += "OR Hadith_Book_Number = 4 ";
           }
           if (clbBooksName.GetItemChecked(4) == true)
           {
               BookSelection += "OR Hadith_Book_Number = 5 ";
           }
            if (clbBooksName.GetItemChecked(5) == true)
           {
               BookSelection += "OR Hadith_Book_Number = 6 ";
           }
           foreach (DataGridViewRow Datarow in dataGridView1.Rows)
           {

               if (dataGridView1.RowCount == 1) //When DataGridview Contain only one row
               {
                   if (Datarow.Cells[0].Value != null && Datarow.Cells[1].Value != null && Datarow.Cells[2].Value != null)//&& Datarow.Cells[3].Value != null)
                   {

                       WhereClause += Datarow.Cells[0].Value.ToString() + "  " + Datarow.Cells[1].Value.ToString() + " " + Datarow.Cells[2].Value.ToString() + " ";
                       SearchQuery = BookSelection + " AND " + WhereClause;
                   }

                   else
                   {
                       MessageBox.Show("Must Fill Column-کالم لازمی پر کریں","Error",MessageBoxButtons.OK,MessageBoxIcon.Information);
                   }

               }
               else if (dataGridView1.RowCount > 1) //When DataGridview Contain MOre then 1 row
               {
                   if (Datarow.Cells[0].Value != null && Datarow.Cells[1].Value != null && Datarow.Cells[2].Value != null && Datarow.Cells[3].Value != null)
                   {

                       WhereClause += Datarow.Cells[0].Value.ToString() + "  " + Datarow.Cells[1].Value.ToString() + " " + Datarow.Cells[2].Value.ToString()+ " " + Datarow.Cells[3].Value.ToString()+ " ";
                       SearchQuery = BookSelection + " AND " + WhereClause;
                   }

                   else
                   {
                       MessageBox.Show("Must Fill Column-کالم لازمی پر کریں", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                   }

               }

           }


           //for testing search query ! What is this?
           MessageBox.Show(SearchQuery);


           //passing value to BLL CLass
           objSearching.WhereClause = SearchQuery;

           //pass Searched values to Form1 (Main Form) of Application.Bind also Search's tab Combobx and textboxes
           //this work require to do!!!!!
           FrmMain.dgvhadiths.DataSource = objSearching.HadithSearching();

           dgvSearchHadith.DataSource = objSearching.HadithSearching();
           //reset the searching query variable
           WhereClause = ""; SearchQuery = ""; BookSelection="";
       }


But I want To Send Directly Where Clause to SP. YOu can See SP already contain Where keyword But i dont want this. I want to Send Where clause on the basis of Checklist box item selection
Posted
Comments
Then make one SP parameter and send that.

1 solution

Hi ,

Try Doing Something like this ?

SQL
BEGIN
declare @SQL VARCHAR(MAX)
SET @SQL='Select [Hadith_Text]
      ,[Hadith_Urdu]
      ,[Hadith_English]
      ,[Chapter_English_Name]
      ,[Chapter_Urdu_Name]
      ,[Baab_English_Name]
      ,[Baab_Urdu_Name]
      ,[Baab_Id]
      ,[Hadith_Book_Number]
	  From Hadiths_old
	  --Where ID < 34192 '
 
IF @WhereClause<>''
	SET @SQL=@SQL+'  '+'Where ID < 34192 AND'+ @WhereClause + ' order by ID'
ELSE
	SET @SQL=@SQL+'  '+'Where ID < 34192  order by ID'
EXEC(@SQL)


And In Your C# Code , Optimize it little bit ... I think You can do something like the following ...

On Your Button Click...

C#
StringBuilder BookSelection = new StringBuilder();

foreach (int item in checkedListBox1.CheckedIndices)
{
    if (BookSelection.Length == 0)
        BookSelection.Append("Hadith_Book_Number In (");
    BookSelection.Append(" '");
    BookSelection.Append((item+1).ToString());
    BookSelection.Append("' ");
}

if (BookSelection.Length > 0)
    BookSelection.Append(")");
 
Share this answer
 
v2

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