GridView from Stored Procedure
In this article, you can see how to populate a gridview from stored procedure.
Stored Procedure
A stored procedure is a group of Transact-SQL statements compiled into a single execution plan. Stored procedures are more secure than SQL queries code and often get result faster.
Database We are using SQL DATABASE:-
Grid View Binding using Stored Procedure
You can retrieve data from database using this Store Procedure and display it in a GridView.
Default.aspx:-
C# Source Code:-
Stored Procedure
A stored procedure is a group of Transact-SQL statements compiled into a single execution plan. Stored procedures are more secure than SQL queries code and often get result faster.
Database We are using SQL DATABASE:-
Create table tblRecord ( Id int primary key, Name varchar(200),Branch varchar(50) ) --Insert some record --USER STORE PROCETURE create proc USP_SelecttblRecord As Begin Select * from tblRecord End
Grid View Binding using Stored Procedure
You can retrieve data from database using this Store Procedure and display it in a GridView.
Default.aspx:-
C# Source Code:-
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; public partial class Default7 : System.Web.UI.Page { SqlConnection con = new SqlConnection("Data source=PS-PC\\SANTOSH; Database=dbSantoshTest; Integrated Security=true"); //connection is created..... protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { loadRecord(); } } protected void loadRecord() //bind gridview..... { con.Open(); SqlCommand cmd = new SqlCommand("USP_SelecttblRecord", con); cmd.CommandType = CommandType.StoredProcedure; SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); int count = ds.Tables[0].Rows.Count; con.Close(); if (ds.Tables[0].Rows.Count > 0) { gvDetail.DataSource = ds; gvDetail.DataBind(); } else { ds.Tables[0].Rows.Add(ds.Tables[0].NewRow()); gvDetail.DataSource = ds; gvDetail.DataBind(); int columncount = gvDetail.Rows[0].Cells.Count; } } }