In
this tutorial I will explain how to bind the Gridview Stored Procedure using Linq
to Sql
In
the previous article I have explained Insert Data into Database using Storeprocedure in Linq to sql, Increase the maximum upload file size, what is LINQ?Its advantage, disadvantage and types and How to insert data into databaseusing LINQ.
Implementation:
I
have created a table Tb_Student and insert records to it.
Create
a store procedure to Get data from database
Create Proc
Sp_GetStudentData
As begin
Select * from Tb_Student
end
Add
a webform to project/website. Drag and drop the required controls from toolbox
to webform.
HTML
Markup of webform:
<asp:GridView ID="grdstudent" runat="server" AutoGenerateColumns="false" AllowPaging="true" PageSize="4">
<Columns>
<asp:BoundField DataField="Student_Name" HeaderText="Name" />
<asp:BoundField DataField="Student_Address" HeaderText="Address" />
<asp:BoundField DataField="RollNo" HeaderText="Roll" />
</Columns>
</asp:GridView>
Create
object of ContextData of the DBML.
C#:
ProjectDataClassesDataContext db = new ProjectDataClassesDataContext();
VB:
Dim db As New ProjectDataClassesDataContext
Write
the code on bind Gridview.
C#:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindGrid();
}
}
public void BindGrid()
{
grdstudent.DataSource = db.Sp_GetStudentData();
grdstudent.DataBind();
}
protected void
grdstudent_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdstudent.PageIndex = e.NewPageIndex;
BindGrid();
}
VB:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGrid()
End If
End Sub
Public Sub BindGrid()
grdstudent.DataSource = db.Sp_GetStudentData
grdstudent.DataBind()
End Sub
Protected Sub
grdstudent_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles grdstudent.PageIndexChanging
grdstudent.PageIndex = e.NewPageIndex
BindGrid()
End Sub
Now
build, run the project and check out the result.
Result:
In this article we have learn How to Bind gridview with Store procedure using LINQ to SQL (C#, VB). I hope you enjoyed this article.
No comments:
Post a Comment