Showing posts with label Store Procedure. Show all posts
Showing posts with label Store Procedure. Show all posts

Wednesday, September 25, 2013

linq using Store Procedure

Add class……

using System;
using System.Data.Linq;
using System.Data.Linq.Mapping;

[Table(Name="student")]
public class E_modal
{
    [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; }
      
}

.aspx page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq;

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DataContext d = new DataContext("initial catalog=batch4;data source=dev-pc;integrated security=yes");
       Table< E_modal > e1= d.GetTable< E_modal >();
       GridView1.DataSource = e1;
       GridView1.DataBind();
    }
}


 

Using stored procedure


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; }

}

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;
    }

}


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();
    }
}




[3/29/2010 7:52:12 PM] sandeep karan: http://msdn.microsoft.com/en-us/library/ms998549.aspx
[3/29/2010 9:15:56 PM] sandeep karan: using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using System.Data.Linq;
using System.Data.Linq.Mapping;     

public class Class2:DataContext
{
 public Class2(string c):base(c)

 {
 
 }
    [Function(Name="sel_prod")]
    public ISingleResult<Class1> show()
    {
      IExecuteResult res=this.ExecuteMethodCall(this,(MethodInfo)MethodInfo.GetCurrentMethod());
      ISingleResult<Class1> s = (ISingleResult<Class1>)res.ReturnValue;
      return s;
    }
}
[3/29/2010 9:20:01 PM] sandeep karan: using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Data.Linq;
using System.Data.Linq.Mapping;

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

    }


    [Function(Name="usp_SelectCustomer")]
    public ISingleResult<st>  getdata()
    {
       IExecuteResult obj=this.ExecuteMethodCall(this, (MethodInfo)MethodInfo.GetCurrentMethod());
       ISingleResult<st> ret=(ISingleResult<st>)obj.ReturnValue;
       return ret;
    }
    [Function(Name = "customerbyid")]
    public ISingleResult<st> getdataid([Parameter(Name = "cid", DbType = "Int")] System.Nullable<int> id)

    {
        IExecuteResult obj = this.ExecuteMethodCall(this, (MethodInfo)MethodInfo.GetCurrentMethod(),id);
        ISingleResult<st> ret = (ISingleResult<st>)obj.ReturnValue;
        return ret;
    }
   
    [Function(Name = "insertc")]
    public int InsertCustomer([Parameter(Name = "Cid", DbType = "Int")]
 int  CustId, [Parameter(Name = "Cname",DbType = "nvarchar(20)")]string custname)
    {
        IExecuteResult result = this.ExecuteMethodCall(this,
        ((MethodInfo)(MethodInfo.GetCurrentMethod())), CustId, custname);
        return (int)result.ReturnValue;
    }
}


Tuesday, October 23, 2012

Error Handling in SQL Server 2005 Stored Procedure using Try Catch Block


Error Handling in SQL Server 2005 Stored Procedure using Try Catch Block


I am fascinated by the easy and safer way SQL Server 2005 provides for error handling. Traditionally we used @@ERROR for this purpose, along the BEGIN TRANSACTION, COMMIT TRANSACTION, ROLLBACK TRANSACTION blocks. Below is one example.
Traditional Error Handling in SQL Server 2000/2005
ALTER PROCEDURE dbo.DeleteFromMyTable
AS
/* SET NOCOUNT ON */
BEGIN TRANSACTION
DELETE FROM myTable1 WHERE myColumn='myColumnValue'--delete transaction1
IF @@ERROR>0 --some error occured
BEGIN
ROLLBACK TRANSACTION 
RETURN
END
ELSE --no error has occured till now, move ahead (do some more transactions)
BEGIN
BEGIN TRANSACTION
DELETE FROM myTable2 WHERE myColumn='myColumnValue'--delete transaction2
IF @@ERROR>0 --some error occured at this point
BEGIN
ROLLBACK TRANSACTION 
--this rollbacks previous delete action also
RETURN
END
ELSE 
--both the delete transactions successful !
BEGIN
--so commit the transactions
COMMIT TRANSACTION
RETURN
END
END
All these stuffs work (perfectly!). But oh! SQL Server 2005! There exists more easy and interesting way. If you are an asp.net programmer or the one who works with front end, there is exactly like what a developer faces with error handling. Guess what? You are right, it is Try..Catch block. Yes Try..Catch block has been introduced in SQL Server 2005, and it provides better way to performing transactions like above in SQL Server database.
Latest Error Handling in SQL Server 2005
ALTER PROCEDURE dbo.DeleteFromMyTable
AS
/* SET NOCOUNT ON */
BEGIN TRY
BEGIN TRANSACTION
DELETE FROM myTable1 WHERE myColumn='myColumnValue'--delete transaction1
DELETE FROM myTable2 WHERE myColumn='myColumnValue'--delete transaction2
COMMIT TRANSACTION
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0 --some error has occurred
ROLLBACK TRAN --so rollback all transactions
--you raise error or fill output variable here
--for front end reference in your asp.net webpage
END CATCH 
See how easy and better way? I found this Try..Catch has made sql programming like front end programming. Happy Programming!

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>