Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Creating a Search Control

Creating a Search Control

Scheduled Pinned Locked Moved ASP.NET
databasewpfwcfsysadminquestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    scotchy2hotty2k2
    wrote on last edited by
    #1

    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(); } }

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups