C# .NET - Search textbox needed - Asked By Elvin on 12-Feb-12 11:43 PM

I have tried to have the gridview display the infromation from database table in SQL 2008, but it os not working. Any hints or sample?
Web Star replied to Elvin on 12-Feb-12 11:59 PM
you query is not clear what you want to do actually if you want to put search textbox on you page and than get search result from database display into gridview than you need to fill DataSet based on like query on search text input in the search textbox and than bind that dataset to gridview. 
Somesh Yadav replied to Elvin on 13-Feb-12 01:49 AM
HI,
Try the below code.
//add namespace using System.Data.SqlClient;
SqlConnection sqlcon = new SqlConnection(strsql);

//here i use the query
//create the object of sqlcommand
SqlCommand sqlcmd = new SqlCommand("select * from [User]", sqlcon);

//create sqldataadapter object and give the sqlcommand as parameter
SqlDataAdapter adp = new SqlDataAdapter(sqlcmd);
//declare the dataset
DataSet ds = new DataSet();
//fill dataset using fill method of SqlDataAdapter
adp.Fill(ds);

//bind the GridView1
GridView1.DataSource = ds.Tables[0];

GridView1.DataBind();
dipa ahuja replied to Elvin on 13-Feb-12 02:46 AM
protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
    bindGrid();
  }
}
void bindGrid()
{
  SqlDataAdapter da = new SqlDataAdapter("select * from TableName", "Connection String");
  DataTable dt = new DataTable();   
  da.Fill(dt);
  GridView1.DataSource = dt;
  GridView1.DataBind();
}