Learning Horizon | For Learners

ASP.NET, SQL SERVER, JQUERY,JAVASCRIPT, WEBSPHERE

Friday 24 August 2012

Bind DataGridView From SQl Server Database Table Using C#

In this tutorial I am going to show you how to get value from database and show/bind in in gridview.

Step 1:-

First of all open a new window form project in your visual studio.

Step 2:-


Drag and drop a gridview control into your form and assign it a name for example “personDataGridView”. Also drag a button in your form and assign it a name “personButton”.

Step 3:-


Now just write the below mentioned code in your event handler.


   Private void personButton_click(object sender, Eventargs e){
   SqlConnection con = 
   new SqlConnection ("Type your connection string here"); 
      try
            {
                con.Open();
       }
            catch (SqlException ex)
            {
                throw ex;
            }
     SqlDataAdapter da = 
     new SqlDataAdapter("select * from person, con);
            DataSet dt = new DataSet();
            da.Fill(dt, "person");
            practiceServerGridview.DataSource = dt;
            practiceServerGridview.DataMember = "person";

            con.Close();
     }
   

No comments:

Post a Comment

Please do not enter spam links.