views:

101

answers:

4

I have a Gridview that I want to filter. My gridview is in an update panel and my filter button is not. On button click i have the following

protected void bttnfilter_Click(object sender, ImageClickEventArgs e)
{
    if (TextBox1.Text != "")
    {
        SqlDataSource1.SelectCommand += " and field like '%' + @param + '%'";
        SqlConnection conn = new SqlConnection(SqlDataSource1.ConnectionString );
        SqlCommand cmd = new SqlCommand(SqlDataSource1.SelectCommand, conn);
        cmd.Parameters.Add(new SqlParameter("@param", TextBox1.Text));
    }

}

So i add to the select command but I'm getting an error. How should I go about this? I don't want to use dynamic sql.

The Error Reads Incorrect Syntax near 'and'

+3  A: 

Most probably you are appending to a query without WHERE clause:

SELECT  *
FROM    mytable
        AND field LIKE '%test%'

will give you this error.

Make you query stub as follows:

SELECT  *
FROM    mytable
WHERE   1 = 1

, which will select everything unless you append additional conditions to it.

In your case, this will give:

SELECT  *
FROM    mytable
WHERE   1 = 1
        AND field LIKE '%test%'

, which is a valid query.

I don't want to use dynamic sql.

You are using it.

You better rewrite your query as this:

SELECT  *
FROM    mytable
WHERE   @param IS NULL
UNION ALL
SELECT  *
FROM    mytable
WHERE   field LIKE '%' + @param + '%'

and bind NULL if your text field is empty.

Quassnoi
+1 Most probable
Lukasz Lysik
A: 

Possibly either the lack of a where clause, or SelectCommand is referencing a stored procedure.

CodeByMoonlight
+2  A: 

Are you sure that the last clause in your SQL statement is a WHERE clause? If there is no WHERE clause or if there's another clause after the WHERE (GROUP BY, ORDER BY) then you'll get this error.

Jeff Hornby
You are right. It was an order by. But I changed it and it is saying that it can't find the scalar variable @param.
Eric
+1  A: 

Something like this should do it

private void bttnfilter_Click(object sender, ImageClickEventArgs e)
{
    string filterText = TextBox1.Text.Trim().ToLower();

    if (!String.IsNullOrEmpty(filterText))
        SqlDataSource1.FilterExpression = 
            string.Format("field LIKE '%{0}%'",filterText);         
}
Russ Cam
It's saying it can't find my column field
Eric
@Eric - this assumes that field is the name of a field in a database *table*. You will need to change the name to the name of the field in question
Russ Cam
Russ thank you. I had it as v.field. I changed it to Field. It worked just like you said. +1 also. thanks
Eric
No probs, happy to help :)
Russ Cam
Russ. Could you explain to me the string.Format part of this and how the {0} works? I would like to understand this a little better.
Eric
{0} is a placeholder in the format string for the ToString() representation of the first object in the object args that follow the format string. So, the output of `string.Format("{0} {1} {2}", myObject1, myObject2, myObject3)` would be equilvalent to the output of `myObject1.ToString() + " " + myObject2.ToString() + " " + myObject3.ToString()` . A lot easier to read isn't it (I'm not 100% on the performance of it compared to concatenation, perhaps someone who know for certain can answer that?)
Russ Cam
why do you think i am having an issue using numbers? i get this error:Cannot perform '=' operation on System.String and System.Int32.this is the code:SqlDataSource1.FilterExpression = string.Format("MANUFACTURECODE = '%{0}%'", int.Parse (filterTextMC));
Eric
sorry code looks like thisSqlDataSource1.FilterExpression += string.Format("MANUFACTURECODE = {0}", int.Parse (filterTextMC));
Eric
Because you can't call int.Parse on that text in the object arguments (which is only going to have .ToString() called on it anyway). Remember that the string you're building is a SQL string where numerical values will be represented as unquoted strings to be executed by the DB engine
Russ Cam
Also, if you're adding to an existing FilterExpression then you'll need to prefix with AND . The FilterExpression translates to a WHERE clause essentially
Russ Cam
YEah. I figured it out right after I asked. Thanks for your help Russ. Expect Points soon.
Eric