Trying to convert SQLParameter into string for FilterExpression

Hi, I wanted to prevent sql injection when using a filterexpression. I tried this method and for some reason I cannot get the parameter to show up in the filter expression properly.
protected void Apply_Filter()
        TextBox ProjSearchTxtSRCH = (TextBox)form1.FindControl("ProjSearchTxt");
        TextBox AgmntSearchtxtSRCH = (TextBox)form1.FindControl("AgmntSearchtxt");
        TextBox TNumberSearchtxtSRCH = (TextBox)form1.FindControl("TNumberSearchtxt");
        TextBox GISREFSearchtxtSRCH = (TextBox)form1.FindControl("GISREFSearchtxt");
        SqlConnection conn = new SqlConnection(GetConnectionString());
        if (AgmntSearchtxtSRCH.Text.Length > 0)
            SqlCommand MainSearch = new SqlCommand("Ag_Number = @AgreementNumberSrch",conn);
            SqlParameter AgrmntNumber = new SqlParameter();
            AgrmntNumber.ParameterName = "@AgreementNumberSrch";
            AgrmntNumber.Value = AgmntSearchtxtSRCH.Text;
            ViewState["ShowAll"] = false;

              Object val = MainSearch.CommandText;
              string s = val.ToString();
              QCGridview1.FilterExpression = s;
              Label LableTest = (Label)form1.FindControl("Example");
              LableTest.Text = s;
              ViewState["filter"] = s;
            catch (SqlException ex)
                Label ErrorLabel = (Label)form1.FindControl("ErrorMsg");
                ErrorLabel.Text = ex.Message;

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I think you can do the following:

string s = string.Format( "Ag_Number = {0}", AgmntSearchtxtSRCH.Text );
QCGridview1.FilterExpression = s;

If you want to prevent SQL injection use a regular expression to validate the user input.
see "How To: Use Regular Expressions to Constrain Input in ASP.NET" at

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GravitaZ24Author Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.