Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

Using Stored Procedure In ASP Dot Net

Article:
Viewed:  800 
Posted On:  28/03/2015 05:42:45 
How we can use a Stored Procedure in ASP.NET C# ? 

Here I am showing how we can use a Stored procedure in asp.net C#.

Below is my Stored Procedure.

CREATE PROCEDURE [dbo].[GetAllStudent]
AS
BEGIN
      SET NOCOUNT ON;
     
    SELECT * FROM Student
 
END

My ASPX Is:

<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Using Stored Procedure In ASP.NET</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GVStudent" runat="server" AutoGenerateColumns="False" OnPageIndexChanging="GVStudent_PageIndexChanging"
            CellPadding="4" ForeColor="#333333" GridLines="None" Width="90%">
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
            <Columns>
                <asp:BoundField DataField="Student_ID" HeaderText="Student ID" HeaderStyle-Width="15%">
                    <HeaderStyle Width="15%" HorizontalAlign="Left"></HeaderStyle>
                </asp:BoundField>
                <asp:BoundField DataField="Name" HeaderText="Name" HeaderStyle-Width="15%">
                    <HeaderStyle Width="15%" HorizontalAlign="Left"></HeaderStyle>
                </asp:BoundField>
                <asp:BoundField DataField="Email" HeaderText="Email" HeaderStyle-Width="15%">
                    <HeaderStyle Width="15%" HorizontalAlign="Left"></HeaderStyle>
                </asp:BoundField>
                <asp:BoundField DataField="Class" HeaderText="Class" HeaderStyle-Width="15%">
                    <HeaderStyle Width="15%" HorizontalAlign="Left"></HeaderStyle>
                </asp:BoundField>
                <asp:BoundField DataField="EnrollYear" HeaderText="EnrollYear" HeaderStyle-Width="40%">
                    <HeaderStyle Width="15%" HorizontalAlign="Left"></HeaderStyle>
                </asp:BoundField>
            </Columns>
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>

My ASPX.CS Is:

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;
using System.Configuration;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            BindEmployeeData();
    }
 
    protected void BindEmployeeData()
    {
        using (SqlConnection con = new SqlConnection(@"Server=.\MSSQLServer2k8;database=TestDB;UID=sa; pwd=Password;"))
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "[GetAllStudent]";
            cmd.Connection = con;
            try
            {
                con.Open();
                GVStudent.EmptyDataText = "No Records Found";
                GVStudent.DataSource = cmd.ExecuteReader();
                GVStudent.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
        }
    }
 
    protected void GVStudent_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GVStudent.PageIndex = e.NewPageIndex;
        BindEmployeeData();
    }
 
}


1.png

Image 1.

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us