Wednesday, 10 April 2013

WORKING GRIDVIEW BINDING STOREDPROCEDURE


 Working GRIDVIEW BINDING  STOREDPROCEDURE


Table Design:
TableDesignData In Table:
DatainTable

STORED PROCEDURE:

create procedure sp_GetCustomers
as
begin
select from dbo.customer
end
Code in Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>
Code in Default.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=SourceName;Initial Catalog=DbName;User ID=sa;Password=YourPassword");
        SqlCommand cmd = new SqlCommand("sp_GetCustomers", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
}
OutPut:
Dynamic Gridview binding in asp.net c# by getting data from StoredProcedure

No comments:

Post a Comment