Newbie alert!
Error:
Must declare the scalar variable "@param2".
Must declare the scalar variable "@param2"
(twice for two param2's)
protected void Button1_Click(object sender, EventArgs e)
{
SqlDataSource ds1 = new SqlDataSource(GetConnectionString(), GetSelectionString());
GridView1.DataSource = ds1;
GridView1.DataBind();
}
and
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(); }
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?
Using VS2008 with MS SQL server 2008 R2 connection.
EDIT1: GIVING THE COMPLETE CODE.::
protected string GetSelectionString()
{
string SearchString = "%";
SearchString = SearchString+ TextBox1.Text.Trim().ToString();
SearchString =SearchString+ "%";
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();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlDataSource ds1 = new SqlDataSource(GetConnectionString(), GetSelectionString());
GridView1.DataSource = ds1;
GridView1.DataBind();
}
please note: I am binding it to a GridView control. This WORKS if I hardcode the value of the @param2 in the query.
EDIT2: A DIFFERENT APPROACH WITH DIFFERENT ERROR:
tried it this way,
SqlCommand temp1 = GetSelectionString();
string temp2 = temp1.CommandText.ToString();
SqlDataSource ds1 = new SqlDataSource(GetConnectionString(), temp1.ToString());
GridView1.DataSource = ds1;
GridView1.DataBind();
....getting new error
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
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.