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)
        {
            conn.Open();
            SqlCommand MainSearch = new SqlCommand("Ag_Number = @AgreementNumberSrch",conn);
            SqlParameter AgrmntNumber = new SqlParameter();
            AgrmntNumber.ParameterName = "@AgreementNumberSrch";
            AgrmntNumber.Value = AgmntSearchtxtSRCH.Text;
            MainSearch.Parameters.Add(AgrmntNumber);
            ViewState["ShowAll"] = false;
            
            try
          {

              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;
            }
            finally
            {
                conn.Close();
            }
        }

Open in new window

GravitaZ24Asked:
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.

alb66Commented:
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.
0
alb66Commented:
see "How To: Use Regular Expressions to Constrain Input in ASP.NET" at
http://msdn.microsoft.com/en-us/library/ms998267.aspx
0

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:
Thanks!
0
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
ASP.NET

From novice to tech pro — start learning today.