Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a grid view which include a row that contain
1. Combobox for column selection
<big>2. Combobx for searching method (=, like, <, <>, >)</big>
3. Text box for search (what to search)
4. Combobox for operator (AND/OR)

look carefully this code fragment
C#
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 =  WhereClause;
                    }

I am using this to Form a Where Clause. but Gridview's 2nd column which inside Combobox # 2 and it contain (=, like, <, <>, >) which is really problem form me
C#
Datarow.Cells[1].Value.ToString()

this contain =, <>, <, >, and most important like
now how can i use like key word. The above fragment of code working fine with =, <>, <,> but what for Like i want to use with % but i dont know how to it
Posted
Updated 4-Nov-13 21:20pm
v4
Comments
Provide more info. Is it windows form? What exactly is the scenario?
Muhamad Faizan Khan 4-Nov-13 13:50pm    
yes window form

You'll have to treat it specially. Like:
C#
const string LikeMethodName = 'like';
WhereClause += Datarow.Cells[0].Value.ToString() + "  ";
string method = Datarow.Cells[1].Value.ToString();
if (method.Equals(LikeMethodName, StringComparison.OrdinalIgnoreCase))
{
  WhereClause += "like '%" + Datarow.Cells[2].Value.ToString() + "%' ";
}
else
{
  WhereClause += method + " " + Datarow.Cells[2].Value.ToString() + " ";
}
WhereClause += Datarow.Cells[3].Value.ToString() + " ";
SearchQuery = WhereClause;


Assuming that all of the values you are using to construct the query are selections from lists that you define, then this is OK. If any value going into the query is "typed-in" by the user, then this is very vulnerable to SQL injection attacks[^].

In any case, instead of repeated string concatenation, I suggest using System.Text.StringBuilder for assembling a string from multiple pieces.
 
Share this answer
 
v3
Check for "like" in a "if" clause something like below...
C#
if(Datarow.Cells[1].Value.ToString().ToLower().Equals("like"))
{
    WhereClause += Datarow.Cells[0].Value.ToString() + "  " + "like '%" + Datarow.Cells[2].Value.ToString()+ "%' " + Datarow.Cells[3].Value.ToString()+ " ";
}
else
{
    WhereClause += Datarow.Cells[0].Value.ToString() + "  " + Datarow.Cells[1].Value.ToString() + " " + Datarow.Cells[2].Value.ToString()+ " " + Datarow.Cells[3].Value.ToString()+ " ";
}
 
Share this answer
 
v5
Comments
Muhamad Faizan Khan 4-Nov-13 14:02pm    
again if.. will this work sure.?
Try and let me know, it should work. :)
Muhamad Faizan Khan 4-Nov-13 14:26pm    
i check it but it showing me an error incorrect synatx
if (Datarow.Cells[0].Value != null && Datarow.Cells[1].Value != null && Datarow.Cells[2].Value != null)//&& Datarow.Cells[3].Value != null)
{
if(Datarow.Cells[1].Value.ToString().ToLower().Equals("like"))
{
WhereClause += Datarow.Cells[0].Value.ToString() + " "+ Datarow.Cells[1].Value.ToString() + " %" + Datarow.Cells[2].Value.ToString() + "% ";
}
else
{
WhereClause += Datarow.Cells[0].Value.ToString() + " " + Datarow.Cells[1].Value.ToString() + " %" + Datarow.Cells[2].Value.ToString() + "% ";
//WhereClause += Datarow.Cells[0].Value.ToString() + " " + Datarow.Cells[1].Value.ToString() + " " + Datarow.Cells[2].Value.ToString() + " ";
}
SearchQuery = WhereClause;
}
Where is that syntax error exactly?
Muhamad Faizan Khan 4-Nov-13 14:34pm    
I have got it single quotation were missing.

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