Show Data in Grid View using SqlDataSource Control

The SqlDataSource Data control enables us to use a Web control to access data from any relational data base that supported by ADO.NET. It uses with other controls that display data, such as GridView, FormView and DetailsView control, to display and manipulate data on an ASP.NET Web Form, using little code or no code. It uses ADO.NET classes to interact with relational data base. It works

·         Open the connection with relational data base.

·         Execute the SQL statements for table and view or store procedure

·         Returns selected data

·         Close the connection 

S.No

Property

Description

1.

ConnectionString

 

It is a string. It is containing server name (hostname or IP address), database name, user and password.

2.

SelectCommand

 

This property represents an SQL query or the name of a stored procedure, and is used by the Select method to retrieve data from the underlying database.

SqlDataSource Design Code

<asp:SqlDataSource ID="EmployeeDataSource" runat="server"
        ConnectionString="Data Source=sandeepss-PC; database=development; user=sa;password=knowdev"
         SelectCommand="select Id, Name, Emp_Code,Emp_Age from EMPLOYEE">
  </asp:SqlDataSource>

GridView Design

DataSourceID property set by SqlDataSource ID to bind grid view.

<asp:GridView ID="gvCustomres" runat="server"
            DataSourceID="EmployeeDataSource"
            AutoGenerateColumns="False"
            GridLines="None"
            AllowPaging="true"
            CssClass="mGrid"
            PagerStyle-CssClass="pgr"
            AlternatingRowStyle-CssClass="alt"
            Width=40%>
            <Columns>
                <asp:TemplateField HeaderText="S.No." ItemStyle-HorizontalAlign="Center">
                    <ItemTemplate>
                        <%#Container.DataItemIndex+1%>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Name" HeaderText="Employee Name" />
                <asp:BoundField DataField="Emp_Code" HeaderText="Employee Code" />
                <asp:BoundField DataField="Emp_Age" HeaderText="Employee Age" />               
            </Columns>
        </asp:GridView>

1.png