Creating a Search Control
-
Hi all I want to create a very generic search control which i can use it on any page.so as to search the data from the table provided by user,list of columns and the sql will be dynamically created depending on textbox id and there respective values. Please Suggest how do i create a server control so that i can use it on any page which will be either return dynamic sql or datasource for binding with gridview. protected void Button1_Click(object sender, EventArgs e) { string conn=server=localhost;database=MyData;uid=sa;password=password"; SqlConnection sconn = new SqlConnection(); sconn.ConnectionString = conn; ArrayList ar = new ArrayList(); ArrayList ar1 = new ArrayList(); foreach (Control c in Panel1.Controls) { Type t = c.GetType(); string id; string value; string name = t.Name; TextBox t1 = null; if (name.Equals("TextBox")) { id = c.ClientID; ar.Add(id); t1 = (TextBox)c; if (t1.Text.Length>0) { value = t1.Text; } else { value = "%"; } ar1.Add(value); } } bool lbHasWhere = false; StringBuilder loBuffer = new StringBuilder(); loBuffer.Append("Select '"+columns+"' From '" + tablename+ "'"); for (int k = 0; k < ar.Count; k++) { if (lbHasWhere) loBuffer.Append(" And "); else { loBuffer.Append(" Where "); lbHasWhere = true; } loBuffer.Append("" + ar[k] + " like '" + ar1[k] + "'"); } SqlCommand scomm = new SqlCommand(); scomm.CommandText = loBuffer.ToString(); scomm.Connection = sconn; sconn.Open(); SqlDataReader sdr = scomm.ExecuteReader(); GridView1.DataSource = sdr; GridView1.DataBind(); sconn.Close(); } }