title: Must declare the scalar variable @param problem
body: <p>Newbie alert!</p>
  
  <p>Error:</p>
  
  <p><code>
  Must declare the scalar variable "@param2".</p>
  
  <p>Must declare the scalar variable "@param2"
  </code>
  (twice for two param2's)
  <code><pre>
  protected void Button1_Click(object sender, EventArgs e)
  {
     SqlDataSource ds1 = new SqlDataSource(GetConnectionString(), GetSelectionString());
     GridView1.DataSource = ds1;
     GridView1.DataBind();
  }</pre></code></p>
  
  <p>and</p>
  
  <p><code></p>
  
  <pre>
   protected string GetSelectionString()
      {
          string SearchString = TextBox1.Text.ToString();
          if (RadioButtonList1.SelectedValue == "ALL")
          {
              SqlParameter @param2 = new SqlParameter();
              SqlCommand SearchAll = new SqlCommand("SELECT Document_Name, Document_Summary FROM Document_Details WHERE (Document_Id IN (SELECT Document_Id FROM Search_Index WHERE (Tag_Id IN (SELECT DISTINCT Tag_Id FROM Tags WHERE (Tag_Name LIKE '%'+@param2+'%'))))) UNION SELECT Document_Name, Document_Summary FROM Document_Details AS Document_Details_1 WHERE (Document_Name LIKE '%'+@param2+'%')");
             SearchAll.Parameters.AddWithValue("@param2", SearchString.ToString());
              return (string)SearchAll.CommandText.ToString();
          }
  </pre>
  
  <p></code></p>
  
  <p>TextBox1 value will be passed by user. I have searched solutions for around 6 hours... and still stuck up with this problem. Any solutions please?</p>
  
  <p>Using VS2008 with MS SQL server 2008 R2 connection.</p>
  
  <p><b>EDIT1: GIVING THE COMPLETE CODE.::</b>
  <PRE>
  <CODE></p>
  
  <p>protected string GetSelectionString()
      {
          string SearchString = "%";
          SearchString = SearchString+ TextBox1.Text.Trim().ToString();
          SearchString =SearchString+ "%";</p>
  
  <pre><code>    if (RadioButtonList1.SelectedValue == "ALL")
      {
          SqlParameter @param2 = new SqlParameter();
          SqlCommand SearchAll = new SqlCommand("SELECT Document_Name, Document_Summary FROM Document_Details WHERE (Document_Id IN (SELECT Document_Id FROM Search_Index WHERE (Tag_Id IN (SELECT DISTINCT Tag_Id FROM Tags WHERE (Tag_Name LIKE @param2))))) UNION SELECT Document_Name, Document_Summary FROM Document_Details AS Document_Details_1 WHERE (Document_Name LIKE @param2)");
          SearchAll.Parameters.AddWithValue("@param2", SearchString.ToString());
          return (string)SearchAll.CommandText.ToString();
      }
      if (RadioButtonList1.SelectedValue == "FILENAMES")
      {
          SqlParameter param2 = new SqlParameter();
  
          SqlCommand SearchFileName = new SqlCommand("SELECT Document_Name, Document_Summary FROM Document_Details WHERE (Document_Name LIKE @param2)");
          SearchFileName.Parameters.AddWithValue("@param2", SearchString.ToString());
          return (string)SearchFileName.CommandText.ToString();
      }
  </code></pre>
  
  <p>protected void Button1_Click(object sender, EventArgs e)
      {
          SqlDataSource ds1 = new SqlDataSource(GetConnectionString(), GetSelectionString());
          GridView1.DataSource = ds1;
          GridView1.DataBind();
      }
  </PRE>
  </CODE></p>
  
  <p>please note: I am binding it to a GridView control. This WORKS if I hardcode the value of the @param2 in the query.</p>
  
  <p><b>EDIT2: A DIFFERENT APPROACH WITH DIFFERENT ERROR:</B>
  <CODE><PRE>
  tried it this way, 
  SqlCommand temp1 = GetSelectionString();
  string temp2 = temp1.CommandText.ToString();
  SqlDataSource ds1 = new SqlDataSource(GetConnectionString(), temp1.ToString());
  GridView1.DataSource = ds1;
  GridView1.DataBind();
  </PRE></CODE>
  ....getting new error</p>
  
  <p>Could not find server 'System' in sys.servers. Verify that the correct server name was specified. If necessary, execute the stored procedure sp_addlinkedserver to add the server to sys.servers</p>
  
  <p>System.Data.SqlClient.SqlException: Could not find server 'System' in sys.servers. Verify that the correct server name was specified. If necessary, execute the stored procedure sp_addlinkedserver to add the server to sys.servers.</p>