Friday, 24 February 2012

Return value from storedprocedure

Stored Procedure:-


ALTER PROCEDURE TestInsert
(
@RollNo int,
@Name nvarchar(50),
@Gender varchar(10),
@Age int,
@DOB nvarchar(50),
@Address nvarchar(100),
@ContactNo nvarchar(15),
@Status int,
@Uimage nvarchar(100),
@Result int output
)
as
Begin
set @Result=0
if(exists(select RollNo from RegForm where RollNo=@RollNo))
Begin
update RegForm set Name=@Name,Gender=@Gender,Age=@Age,DOB=convert(datetime,@DOB,103),
Address=@Address,ContactNo=@ContactNo,Status=@Status,Uimage=@Uimage where RollNo=@RollNo
set @Result=1
end
else
Begin
  insert into RegForm (RollNo,Name,Gender,Age,DOB,Address,ContactNo,Status,Uimage)
values
(@RollNo,@Name,@Gender,@Age,convert(datetime,@DOB,103),@Address,@ContactNo,@Status,@Uimage)
set @Result=2
end
end

C# Code:-


public int InsertRecord(ObjectLayer.ObjectLayer objobject)
        {
            int result = 0;
            try
            {              
                SqlCommand com = new SqlCommand("TestInsert", con);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@RollNo", objobject.RollNo);
                com.Parameters.AddWithValue("@Name", objobject.Name);
                com.Parameters.AddWithValue("@Gender", objobject.Gender);
                com.Parameters.AddWithValue("@Age", objobject.Age);
                com.Parameters.AddWithValue("@DOB", objobject.DOB);
                com.Parameters.AddWithValue("@Address", objobject.Address);
                com.Parameters.AddWithValue("@ContactNo", objobject.ContactNo);
                com.Parameters.AddWithValue("@Status", objobject.Stat);
                com.Parameters.AddWithValue("@Uimage", objobject.Uimage);

                SqlParameter pa1 = new SqlParameter("@Result", SqlDbType.Int);
                pa1.Direction = ParameterDirection.Output;
                pa1.Size = 100;
                com.Parameters.Add(pa1);
                ExecuteStoredProcedure(com);
                result = Convert.ToInt32(com.Parameters["@Result"].Value.ToString());
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
            }
            return result;
        }

No comments:

Post a Comment