We can use SqlDatasourse to show data in
Gridview data control. In this article we will learn how to do this.
Create a Table in database.
Column Name
|
Datatype
|
EmpId
|
Int(Primary Key. So set Is Identity=True)
|
Name
|
Varchar(500)
|
Age
|
int
|
Now create a web page
in your application where you want to show data in gridview.
Connection string in Web Config:
<connectionStrings>
<add name="con" connectionString="Data Source=localhost; Initial Catalog= test; Integrated
Security=true;" providerName="System.Data.SqlClient"/>
</connectionStrings>
|
Design your page: Drag & drop Gridview
data control and SqldataSourse from
toolbox . Now Configure SqlDataSource , set ConnectionString and write sql
command in SelectCommand property.
Now set SqlDataSource as DataSourseID in Gridview.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"> <title></title></head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="grdEmployee" 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>
</form>
</body>
</html>
|