Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Tuesday, 28 July 2015

In this article I will explain how to display data in gridview  using SqlDataSource. Here I will explain how to bind grid with sqldataSource and show data in gridview without doing any code.


 Implementation: Follow following steps
Create a database i.e. “Test”.  Then create a table “Student_Info”.
Column Name
Datatype
Student_id
Int(Primary Key. So set Is Identity=True)
Student_Name
Varchar(500)
Age
int
Class
Varchar(50)

Now insert some data in this table using “insert” command.
Create Connection: Now create connection in  webconfig file as given below.
<connectionStrings>
    <add name="con" connectionString="Data Source=localhost; Initial Catalog= test; Integrated Security=true;" providerName="System.Data.SqlClient"/>
  </connectionStrings>

Design Section:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

</head>
<body>
    <form id="form1" runat="server">
    <center>
     <fieldset style="width:250px"><legend><strong>Bind Gridview with SqlDataSource</strong></legend>
    <div>
       <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False">
                       <Columns>
                <asp:BoundField DataField="Student_Name" HeaderText="Student name" />
                <asp:BoundField DataField="age" HeaderText="Age" />
                <asp:BoundField DataField="class" HeaderText="Class" />
                </Columns>  
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:con %>" SelectCommand="select * from student_info" runat="server"></asp:SqlDataSource>
      
    </div>
    </fieldset></center>
    </form>
</body>

</html>

0 comments:

Post a Comment