5
votes

I am filtering my gridview using dataview. I am passing the filter command to dataview as mentioned below;

string strFilter= " 0=0 ";

if (Session["SampleSession"] != null)
        {
            strFilter= strFilter+ " and Emp Name = '" + Session["SampleSession"].ToString() + "' ";
        }
dv.RowFilter = strFilter;  // Throws an error here!

It throws an error of Missing operand after 'Operator Name' operator in above line.

i believe there is small error which i am unable to catch.

1
What is strFilter's value when it hits that line? - Matt Hamilton
what is 0=0 in string strFilter= " 0=0 "; ? - Maysam
@Maysam The "0=0" would be a placeholder so that you can easily append "and EmpName = 'Foo'" and still have a valid filter expression. - Matt Hamilton
@Xor My guess right now (until you can give me the value of strFilter) is that your "SampleSession" variable has a single-quote character in it which is breaking the expression. Sort of a poor-man's SQL injection vulnerability. Could you possibly be passing an employee name like "Bob O'Hara"? - Matt Hamilton
@Xor by not responding to the repeated requests for the actual value of strFilter, you wasted a number of people's time trying to answer a question that did not contain the problem. Just a recommendation for the future: post code that actually causes the problem; since you (by definition) don't understand the issue, it is all too easy to accidentally remove the actual cause when posting a question. - Marc Gravell

1 Answers

14
votes

Your problem is that "Emp Name" (the column name) contains a space and needs to be wrapped in square brackets in the filter expression:

strFilter= strFilter+ " and [Emp Name] = '" + Session["SampleSession"].ToString() + "' ";