Friday 2 November 2012

How to use stored procedure in Linq in asp.net using c#.

In the previous post I had explained "How to show table data in gridview using Linq in asp.Net".
In this post ,I have explained How to use stored procedure in linq in asp.net using c#.
Here I have explained step by step.

First create a table in sql server.
Then after create a procedure for this table.
Now open visual studio -> create new web site -> Give a name -> click ok.
Right click on project application -> add new item ->select linq to sql -> give  a name(for example linqToSqlUsingStoredProcedure.dbml).
After click ok button, open a new window -> in new see the right side panel -> drag and drop the procedure from server explorer. Save the .dbml file.
                                                                                                                               
  Now come on design page where you drag and drop a gridview .

Default.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LinqusingStoredProcedure.aspx.cs" Inherits="LinqusingStoredProcedure" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
    </div>
   
    </form>
</body>
</html>

Default.aspx.cs:

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

public partial class LinqusingStoredProcedure : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        linqtosqlusingstoredprocedureDataContext ct = new linqtosqlusingstoredprocedureDataContext();// here linqtosqlusingstoredprocedureDataContext is .dbml file name
        var myreg = ct.registration();
        GridView1.DataSource = myreg;
        GridView1.DataBind();
    }
}

Run the project. Hope you will enjoy.If any query regarding this post write a comment or send a mail at sushilct86@gmail.com

No comments:

Post a Comment

Note: only a member of this blog may post a comment.