How to bind GridView control with SqlDataSource control
During application development there will be many occasions to display database records on a web page. Records are displayed after retrieving them from a Sql database. To do so, we need to use one DataSource control and one DataBound control. We use the DataSource control -SqlDataSource control to access the data from the Sql server database. The DataBound control we use to display the records is the GridView control.
There are two steps to bind a GridView control with SqlDataSource control. The first step is to configure the SqlDataSource control. Configuring the SqlDataSource control involves setting the ConnectionString property and SelectCommand property. These are the minimum steps required to configure the SqlDataSource control.
The next step is to bind the GridView control to the data source by setting its DataSourceID property to the ID of SqlDataSource control. The example below shows how to bind a GridView control to the SqlDataSource control.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$Connectionstrings:ERPConnectionString%>" SelectCommand="SELECT * FROM AccountsTable"> </asp:SqlDataSource> <asp:GridView ID="GridView1" DataSourceID="SqlDataSource1" AutoGenerateColumns="true" runat="server"> </asp:GridView>
The ConnectionString property of the SqlDataSource control specifies how to connect to the database. This property can be a hard-coded connection string or can point to a connection string in a Web.config file as shown in the code given above. The SelectCommand property specifies the SQL statement to execute to retrieve the data.
GridView Articles
- GridView AutoGenerateColumns property
- DataBinding in a GridView control
- Binding a GridView programmatically with dataset
- Binding a GridView programmatically with Generic List Collection
- Using fields with the GridView control
- Adding Boundfields to a GridView control
- Adding button fields to a GridView control
- Create Templatefields in a GridView control
- Access a value using SelectedIndexChanged() event when a row in a GridView is selected
- How to restrict an item from being selected using GridView SelectedIndexChanging Event
- How to add a new row in the GridView control by using the GridView footer template
- Gridview datakeynames
- How to select a particular row in a GridView control using SelectedDataKey method
- Formatting GridView control with CSS
- GridView rowDataBound event
Become an Expert
Learn More
Q1. What is Azure Platform?
GridView CommandField example
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Query entity data model using linq to entities
Difference between arraylist and list collection
How to create a Web service using Visual Studio.net
FormView DataBound Event
Calling base class constructor in C#
Convert a sequence to a generic list using ToList()method
ERP Software Development
Project ideas for students
Accounting Software
Creating an ASP.Net MVC 3 application
Using assemblies in .net applications
How to implement form validation using ASP.Net 2.0 Validation Controls
Constructors in Visual Basic.Net
GridView CommandField example
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Query entity data model using linq to entities
Difference between arraylist and list collection
How to create a Web service using Visual Studio.net
FormView DataBound Event
Calling base class constructor in C#
Convert a sequence to a generic list using ToList()method
ERP Software Development
Project ideas for students
Accounting Software
Creating an ASP.Net MVC 3 application
Using assemblies in .net applications
How to implement form validation using ASP.Net 2.0 Validation Controls
Constructors in Visual Basic.Net