Monday, October 15, 2012

How To Call Store Procedure with Linq in GridView

How To Call Store Procedure with Linq in GridView\

First we can add the two Class First Class name is DAL and second Class name EM(Entity framework).

ADD CLASS DAL
using System;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Reflection;

public class DAL : DataContext
{
    public DAL(string x)
        : base(x)
    {

    }
    [Function(Name = "s_show")]
    public ISingleResult<EM> show()
    {
        IExecuteResult res = this.ExecuteMethodCall(this, (MethodInfo)MethodInfo.GetCurrentMethod());
        ISingleResult<EM> s = (ISingleResult<EM>)res.ReturnValue;
        return s;
    }

}

Add class EM<ENTITY MODAL>
using System;
using System.Data.Linq;
using System.Data.Linq.Mapping;

[Table(Name = "student")]
public class EM
{
    [Column(Name = "roll_no", IsPrimaryKey = true)]
    public int rno { get; set; }

    [Column(Name = "stud_name")]
    public string sn { get; set; }

    [Column(Name = "stud_course")]
    public string sc { get; set; }

    [Column(Name = "stud_fees")]
    public int sf { get; set; }

}

and after Create class we are create or developing FirstEnd Or Gridview 
define is this type


Add Coding .ASPX.CS Page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DAL d = new DAL("initial catalog=batch4;data source=dev-pc;integrated security=yes");
        GridView1.DataSource = d.show();
        GridView1.DataBind();
    }
}

Add Design Page .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">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Home || Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
        <br />
        <br />
    
    </div>
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
    </form>
</body>
</html>




No comments:

Post a Comment