What I want to do is have the first parameter be the column to search and the second be the value to be looking up in that column. Here is the acctual code in question. if I take @SearchItem and replace it with the constant hostname the querey succesfully returns all row's where the hostname = @SearchQuery. What I am attempting to do though is let the user decide what the search on so they could search any of the columns not just hostname. I have a dropdownlist with the possible collumn names.
Code:
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn;
SqlCommand comm;
SqlDataReader reader;
serverDetails.Visible = false;
string connection = ConfigurationManager.ConnectionStrings["serverlist"].ConnectionString;
string sqlquery = "SELECT server_id, hostname, os, description, owner FROM server_info WHERE @SearchItem=@SearchQuery";
conn = new SqlConnection(connection);
comm = new SqlCommand(sqlquery, conn);
String searchQuery = searchBox.Text;
String searchItem = DropDownList1.SelectedValue.ToString();
comm.Parameters.Add("@SearchQuery", System.Data.SqlDbType.VarChar);
comm.Parameters["@SearchQuery"].Value = searchQuery;
comm.Parameters.Add("@SearchItem", System.Data.SqlDbType.VarChar);
comm.Parameters["@SearchItem"].Value = searchItem;
try
{
conn.Open();
reader = comm.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataKeyNames = new string[] { "server_id" };
GridView1.DataBind();
reader.Close();
}
finally
{
conn.Close();
}
}
Bookmarks