Introduction:
In this tutorial we will learn how to call the stored procedure with input/output parameter in tier model in asp.net. In 2-tier you can easily call the stored procedure with in/out parameter but some different tricks are used here to call the stored procedure in n-tier.
For calling a stored procedure with parameter we have to pass the parameter required to procedure. Just same here we are passing parameter as input as well as output. Follow the steps bellow.
Step 1:
Create DataBase Table
Create Table Tbl_Jobs(JobId Int Primary Key,Description Varchar(100))
Step 2:
Create Stored Procedure
Create Procedure Sp_GetJobDesc
(@jobid int input,@desc Varchar(100)output)
as Begin
Select Description From Tbl_Jobs Where JobId=@jobid
End
Step 3:
Write Down this in your Data Access Layer To Call The stored procedure
public static Array
CallStoredProcedure(string[,] InParam, string[,] OutParam, string
_procename)
{
try
{
string
_result;
sqlcn.Open();
sqlcmd = new
SqlCommand(_procename, sqlcn);
sqlcmd.CommandType = CommandType.StoredProcedure;
//InputParaMeter
for
(int i = 0; i < InParam.Length / 2; i++)
{
sqlcmd.Parameters.AddWithValue(InParam[i, 0], InParam[i, 1]);
}
//OutPutParamater
for
(int i = 0; i < OutParam.Length / 2; i++)
{
if
(OutParam[i, 1] == "SqlDbType.Int")
{
sqlcmd.Parameters.Add(OutParam[i, 0], SqlDbType.Int);
}
else
{
sqlcmd.Parameters.Add(OutParam[i, 0], SqlDbType.VarChar,
50);
}
sqlcmd.Parameters[OutParam[i,
0]].Direction = ParameterDirection.Output;
}
sqlcmd.ExecuteNonQuery();
string[,]
_temparray = new string[OutParam.Length
/ 2, 2];
for
(int i = 0; i < OutParam.Length / 2; i++)
{

Join the conversation! Your thoughts help the community grow.