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

What is GridView control and how to bind GridView to data?

What is a GridView control in asp.net? Want to learn how to connect GridView to a database? This article will teach you more about the databound control GridView, properties and events of GridView and binding a GridView control to data.

How to add Buttons in Data GridView?

Want to add buttons to the GridView control in Asp.net? Need to execute a set of statements when the user clicks on the button in the GridView? Learn how to add controls like button to the GridView control and write C# code for user activities performed like Clicking of Buttons in the GridView.

More articles: Gridview databind Gridview data source Gridview

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: