Ramco Ramco

Ramco Ramco

  • 443
  • 3.4k
  • 533.7k

Stored procedure should return value

Aug 17 2024 2:09 PM

Hi

  I have below code and i want if record is successfully added/updated then it should return a value and then success message displayed

using (SqlConnection con = new SqlConnection(Common.CommonFunction.cnn_Live))
                        {
                            SqlCommand cmd = new SqlCommand("Sp_Department", con);
                            cmd.CommandType = CommandType.StoredProcedure;

                            cmd.Parameters.Add("@EntryNo", SqlDbType.Int).Value = hdfDeleteID.Value;
                            cmd.Parameters.Add("@Action", SqlDbType.NVarChar).Value = "D";
                            con.Open();

                            cmd.ExecuteNonQuery();
}
                        

Stored Procedure

Create PROCEDURE [dbo].[sp_Department]
       @Action VARCHAR(1)
	  ,@EntryNo int = Null
      ,@Description VARCHAR(50) = NULL
	  ,@ShortName VARCHAR(25) = NULL
AS
BEGIN
      SET NOCOUNT ON;
 
 
      --INSERT
      IF @Action = 'I'
      BEGIN
        INSERT INTO dbo.Department(Description,createdon,updatedon)
        VALUES (@Description,GETDATE(),GETDATE())
      END
 
      --UPDATE
      IF @Action = 'U'
      BEGIN
        UPDATE  dbo.Department
        SET Description = @Description, updatedon = GETDATE() WHERE docentry = @EntryNo
      END
 
      --DELETE
      IF @Action = 'D'
      BEGIN
        UPDATE dbo.Department
        SET status = 0 , updatedon = GETDATE() WHERE docentry = @EntryNo
      END

END

Thanks


Answers (2)