Code to bind gridview with DataAdapter


Code to bind gridview with DataAdapter using Asp.Net

Note:
->The ConnectionString index value will get from web.config file.


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindRecord();
}

}
Public void bindRecord()
{
SqlConnection con =new SqlConnection(ConfigurationManager.
ConnectionStrings["ConnectionString"].ConnectionString.ToString();)
SqlCommand cmd=new SqlCommand();
cmd.connection=con;
con.open();
cmd.commandText="SELECT * FROM tablename";
SqlDataAdapter da=new SqlDataAdapter(cmd);
Dataset ds=new Dataset();
da.fill(ds);
cmd.ExecuteNonQuery();
grdview.DataSource=ds;
grdview.DataBind();
con.close();


}


Related Articles

More articles: Gridview databind Gridview data source

Comments

Guest Author: jazz05 Feb 2014

use cmd.ExecuteReader();
instead of cmd.ExecuteNonQuery();



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: