Saturday, 14 July 2012

Create procedure with output parameters.




Create procedure with output parameters.

create proc searchrecordemp
@EmpId int,
@EmpName varchar(200) output,
@EmpSalary Decimal(10,2) output
as
begin
select @EmpName=EmpName,@EmpSalary=EmpSalary from tbl_employee
where EmpId=@EmpId
end
---------------------------------------------------------------------------------
declare @empname varchar(200)
declare @empsalary decimal(10,2)
exec searchrecordemp 1, @empname output,@empsalary output
select @EmpName as EMPNAME,@EmpSalary as EMPSALARY

Calling procedure with output parameters from front end using c#.

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
{
    SqlConnection con;
    SqlCommand cmd;
    SqlParameter pr, pr1;
   public _Default()
   {       con = new SqlConnection("initial catalog=PraticeCAC; Data Source=OM-PC; integrated security=yes");
   }    
    protected void Page_Load(object sender, EventArgs e)
    {
        fill();
    }
    public void fill()
    {
        con.Open();
        cmd = new SqlCommand("searchrecordemp", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@EmpId",1);
        pr=cmd.Parameters.Add("@EmpName",SqlDbType.Char,200);
        pr.Direction = ParameterDirection.Output;
        pr1 = cmd.Parameters.Add("@EmpSalary", SqlDbType.Decimal, 10);
        pr1.Direction=ParameterDirection.Output;
        cmd.ExecuteReader();
        Response.Write(pr.Value.ToString()+" "+pr1.Value.ToString());
        con.Close();

    }
}

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home