Tuesday 13 November 2012

How to show data in Gridview using SqlDataSource in Asp.Net .


Hi Friend’s , In this post I have explained  how to use SqlDataSource in GridView to show the Table data.

Introduction:

SqlDataSource connects to data sources, retrieve data from them and then makes this data available for other control to bind to, without the headache of writing code.
Below I have explained step by step with Figure.
First open a new web site in visual studio -> Give  name to it -> click ok.
In design Page drag and drop a GridView Control on it.Click on GridView control , you will see a box at right side on top -> click on this box -> choose new data source . See the figure..

After this choose Sql Datasource.

After it will open Configure data source window. Select your connection from the drop down list, if connection is not available then click on new connection option. To countinue ,click on the next button. In the next screen , pass the sql query to the ‘Configure the select statement ‘ screen as shown in the figure below.

Finally click on Finish Button. This will automatically add the sqlDataSuorce control to the web page.
GridView.aspx:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridView.aspx.cs" Inherits="GridView" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="userName" HeaderText="userName"
                    SortExpression="userName" />
                <asp:BoundField DataField="password" HeaderText="password"
                    SortExpression="password" />
                <asp:BoundField DataField="firstName" HeaderText="firstName"
                    SortExpression="firstName" />
                <asp:BoundField DataField="LastName" HeaderText="LastName"
                    SortExpression="LastName" />
                <asp:BoundField DataField="email" HeaderText="email" SortExpression="email" />
                <asp:BoundField DataField="phoneno" HeaderText="phoneno"
                    SortExpression="phoneno" />
                <asp:BoundField DataField="location" HeaderText="location"
                    SortExpression="location" />
                <asp:BoundField DataField="createdBy" HeaderText="createdBy"
                    SortExpression="createdBy" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:modiConnectionString %>"
            SelectCommand="SELECT [userName], [password], [firstName], [LastName], [email], [phoneno], [location], [createdBy] FROM [m_registration]">
        </asp:SqlDataSource>
   
    </div>
    </form>
</body>
</html>


Press F5 button. If You like this blog ,please spread this blog to all your friends.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.